Fix: out-of-tree dist fails to find test traces
[babeltrace.git] / tests / lib / test_ctf_writer.c
index 608172cf926ce9096ab9623fd3a57c0d9888776b..cd92986b3fd99ee1a278bdc551421f8750ffd425 100644 (file)
@@ -3,7 +3,7 @@
  *
  * CTF Writer test
  *
- * Copyright 2013 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ * Copyright 2013 - 2015 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -26,6 +26,7 @@
 #include <babeltrace/ctf-writer/event.h>
 #include <babeltrace/ctf-writer/event-types.h>
 #include <babeltrace/ctf-writer/event-fields.h>
+#include <babeltrace/ctf-ir/stream-class.h>
 #include <babeltrace/ctf/events.h>
 #include <unistd.h>
 #include <stdlib.h>
@@ -158,12 +159,12 @@ result:
 
                /* Output the metadata and parser output as diagnostic */
                while (getline(&line, &len, metadata_fp) > 0) {
-                       diag("%s", line);
+                       fprintf(stderr, "# %s", line);
                }
 
                rewind(parser_output_fp);
                while (getline(&line, &len, parser_output_fp) > 0) {
-                       diag("%s", line);
+                       fprintf(stderr, "# %s", line);
                }
 
                free(line);
@@ -481,8 +482,9 @@ void append_simple_event(struct bt_ctf_stream_class *stream_class,
        bt_ctf_event_class_put(ret_event_class);
 
        /* Set an event context type which will contain a single integer*/
-       bt_ctf_field_type_structure_add_field(event_context_type, uint_12_type,
-               "event_specific_context");
+       ok(!bt_ctf_field_type_structure_add_field(event_context_type, uint_12_type,
+               "event_specific_context"),
+               "Add event specific context field");
        ok(bt_ctf_event_class_get_context_type(NULL) == NULL,
                "bt_ctf_event_class_get_context_type handles NULL correctly");
        ok(bt_ctf_event_class_get_context_type(simple_event_class) == NULL,
@@ -1479,6 +1481,43 @@ end:
        bt_ctf_event_class_put(event_class);
 }
 
+void test_empty_stream(struct bt_ctf_writer *writer)
+{
+       int ret = 0;
+       struct bt_ctf_trace *trace = NULL;
+       struct bt_ctf_clock *clock = NULL;
+       struct bt_ctf_stream_class *stream_class = NULL;
+       struct bt_ctf_stream *stream = NULL;
+
+       trace = bt_ctf_writer_get_trace(writer);
+       if (!trace) {
+               diag("Failed to get trace from writer");
+               ret = -1;
+               goto end;
+       }
+
+       stream_class = bt_ctf_stream_class_create("empty_stream");
+       if (!stream_class) {
+               diag("Failed to create stream class");
+               ret = -1;
+               goto end;
+       }
+
+       stream = bt_ctf_writer_create_stream(writer, stream_class);
+       if (!stream) {
+               diag("Failed to create writer stream");
+               ret = -1;
+               goto end;
+       }
+end:
+       ok(ret == 0,
+               "Created a stream class with default attributes and an empty stream");
+       bt_ctf_trace_put(trace);
+       bt_ctf_clock_put(clock);
+       bt_ctf_stream_put(stream);
+       bt_ctf_stream_class_put(stream_class);
+}
+
 int main(int argc, char **argv)
 {
        char trace_path[] = "/tmp/ctfwriter_XXXXXX";
@@ -1497,16 +1536,20 @@ int main(int argc, char **argv)
        struct utsname name;
        char hostname[BABELTRACE_HOST_NAME_MAX];
        struct bt_ctf_clock *clock, *ret_clock;
-       struct bt_ctf_stream_class *stream_class;
+       struct bt_ctf_stream_class *stream_class, *ret_stream_class;
        struct bt_ctf_stream *stream1;
        const char *ret_string;
        const unsigned char *ret_uuid;
        unsigned char tmp_uuid[16] = { 0 };
        struct bt_ctf_field_type *packet_context_type,
                *packet_context_field_type,
+               *packet_header_type,
+               *packet_header_field_type,
                *integer_type,
                *stream_event_context_type,
-               *ret_field_type;
+               *ret_field_type,
+               *event_header_field_type;
+       struct bt_ctf_field *packet_header, *packet_header_field;
        struct bt_ctf_trace *trace;
        int ret;
 
@@ -1716,7 +1759,7 @@ int main(int argc, char **argv)
        ok(bt_ctf_stream_class_get_name(NULL) == NULL,
                "bt_ctf_stream_class_get_name handles NULL correctly");
        ret_string = bt_ctf_stream_class_get_name(stream_class);
-       ok(!strcmp(ret_string, "test_stream"),
+       ok(ret_string && !strcmp(ret_string, "test_stream"),
                "bt_ctf_stream_class_get_name returns a correct stream class name");
 
        ok(bt_ctf_stream_class_get_clock(stream_class) == NULL,
@@ -1746,8 +1789,67 @@ int main(int argc, char **argv)
        ok(bt_ctf_stream_class_get_id(stream_class) == 123,
                "bt_ctf_stream_class_get_id returns the correct value");
 
-       /* Create a "uint5_t" equivalent custom packet context field */
-       packet_context_field_type = bt_ctf_field_type_integer_create(5);
+       /* Validate default event header fields */
+       ok(bt_ctf_stream_class_get_event_header_type(NULL) == NULL,
+               "bt_ctf_stream_class_get_event_header_type handles NULL correctly");
+       ret_field_type = bt_ctf_stream_class_get_event_header_type(
+               stream_class);
+       ok(ret_field_type,
+               "bt_ctf_stream_class_get_event_header_type returns an event header type");
+       ok(bt_ctf_field_type_get_type_id(ret_field_type) == CTF_TYPE_STRUCT,
+               "Default event header type is a structure");
+       event_header_field_type =
+               bt_ctf_field_type_structure_get_field_type_by_name(
+               ret_field_type, "id");
+       ok(event_header_field_type,
+               "Default event header type contains an \"id\" field");
+       ok(bt_ctf_field_type_get_type_id(
+               event_header_field_type) == CTF_TYPE_INTEGER,
+               "Default event header \"id\" field is an integer");
+       bt_ctf_field_type_put(event_header_field_type);
+       event_header_field_type =
+               bt_ctf_field_type_structure_get_field_type_by_name(
+               ret_field_type, "timestamp");
+       ok(event_header_field_type,
+               "Default event header type contains a \"timestamp\" field");
+       ok(bt_ctf_field_type_get_type_id(
+               event_header_field_type) == CTF_TYPE_INTEGER,
+               "Default event header \"timestamp\" field is an integer");
+       bt_ctf_field_type_put(event_header_field_type);
+       bt_ctf_field_type_put(ret_field_type);
+
+       /* Add a custom trace packet header field */
+       ok(bt_ctf_trace_get_packet_header_type(NULL) == NULL,
+               "bt_ctf_trace_get_packet_header_type handles NULL correctly");
+       packet_header_type = bt_ctf_trace_get_packet_header_type(trace);
+       ok(packet_header_type,
+               "bt_ctf_trace_get_packet_header_type returns a packet header");
+       ok(bt_ctf_field_type_get_type_id(packet_header_type) == CTF_TYPE_STRUCT,
+               "bt_ctf_trace_get_packet_header_type returns a packet header of type struct");
+       ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
+               packet_header_type, "magic");
+       ok(ret_field_type, "Default packet header type contains a \"magic\" field");
+       bt_ctf_field_type_put(ret_field_type);
+       ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
+               packet_header_type, "uuid");
+       ok(ret_field_type, "Default packet header type contains a \"uuid\" field");
+       bt_ctf_field_type_put(ret_field_type);
+       ret_field_type = bt_ctf_field_type_structure_get_field_type_by_name(
+               packet_header_type, "stream_id");
+       ok(ret_field_type, "Default packet header type contains a \"stream_id\" field");
+       bt_ctf_field_type_put(ret_field_type);
+
+       packet_header_field_type = bt_ctf_field_type_integer_create(22);
+       ok(!bt_ctf_field_type_structure_add_field(packet_header_type,
+               packet_header_field_type, "custom_trace_packet_header_field"),
+               "Added a custom trace packet header field successfully");
+
+       ok(bt_ctf_trace_set_packet_header_type(NULL, packet_header_type) < 0,
+               "bt_ctf_trace_set_packet_header_type handles a NULL trace correctly");
+       ok(bt_ctf_trace_set_packet_header_type(trace, NULL) < 0,
+               "bt_ctf_trace_set_packet_header_type handles a NULL packet_header_type correctly");
+       ok(!bt_ctf_trace_set_packet_header_type(trace, packet_header_type),
+               "Set a trace packet_header_type successfully");
 
        ok(bt_ctf_stream_class_get_packet_context_type(NULL) == NULL,
                "bt_ctf_stream_class_get_packet_context_type handles NULL correctly");
@@ -1768,6 +1870,9 @@ int main(int argc, char **argv)
        ok(bt_ctf_stream_class_set_packet_context_type(stream_class,
                integer_type) < 0,
                "bt_ctf_stream_class_set_packet_context_type rejects a packet context that is not a structure");
+       /* Create a "uint5_t" equivalent custom packet context field */
+       packet_context_field_type = bt_ctf_field_type_integer_create(5);
+
        ret = bt_ctf_field_type_structure_add_field(packet_context_type,
                packet_context_field_type, "custom_packet_context_field");
        ok(ret == 0, "Packet context field added successfully");
@@ -1799,11 +1904,29 @@ int main(int argc, char **argv)
                stream_class);
        ok(ret_field_type == stream_event_context_type,
                "bt_ctf_stream_class_get_event_context_type returns the correct field type.");
+       bt_ctf_field_type_put(ret_field_type);
 
        /* Instantiate a stream and append events */
        stream1 = bt_ctf_writer_create_stream(writer, stream_class);
        ok(stream1, "Instanciate a stream class from writer");
 
+       ok(bt_ctf_stream_get_class(NULL) == NULL,
+               "bt_ctf_stream_get_class correctly handles NULL");
+       ret_stream_class = bt_ctf_stream_get_class(stream1);
+       ok(ret_stream_class,
+               "bt_ctf_stream_get_class returns a stream class");
+       ok(ret_stream_class == stream_class,
+               "Returned stream class is of the correct type");
+
+       /*
+        * Try to modify the packet context type after a stream has been
+        * created.
+        */
+       ret = bt_ctf_field_type_structure_add_field(packet_header_type,
+               packet_header_field_type, "should_fail");
+       ok(ret < 0,
+               "Trace packet header type can't be modified once a stream has been instanciated");
+
        /*
         * Try to modify the packet context type after a stream has been
         * created.
@@ -1811,7 +1934,7 @@ int main(int argc, char **argv)
        ret = bt_ctf_field_type_structure_add_field(packet_context_type,
                packet_context_field_type, "should_fail");
        ok(ret < 0,
-               "Packet context type can't be modified once a stream class has been instanciated");
+               "Packet context type can't be modified once a stream has been instanciated");
 
        /*
         * Try to modify the stream event context type after a stream has been
@@ -1820,18 +1943,48 @@ int main(int argc, char **argv)
        ret = bt_ctf_field_type_structure_add_field(stream_event_context_type,
                integer_type, "should_fail");
        ok(ret < 0,
-               "Stream event context type can't be modified once a stream class has been instanciated");
+               "Stream event context type can't be modified once a stream has been instanciated");
 
        /* Should fail after instanciating a stream (frozen) */
        ok(bt_ctf_stream_class_set_clock(stream_class, clock),
                "Changes to a stream class that was already instantiated fail");
 
+       /* Populate the custom packet header field only once for all tests */
+       ok(bt_ctf_stream_get_packet_header(NULL) == NULL,
+               "bt_ctf_stream_get_packet_header handles NULL correctly");
+       packet_header = bt_ctf_stream_get_packet_header(stream1);
+       ok(packet_header,
+               "bt_ctf_stream_get_packet_header returns a packet header");
+       ret_field_type = bt_ctf_field_get_type(packet_header);
+       ok(ret_field_type == packet_header_type,
+               "Stream returns a packet header of the appropriate type");
+       bt_ctf_field_type_put(ret_field_type);
+       packet_header_field = bt_ctf_field_structure_get_field(packet_header,
+               "custom_trace_packet_header_field");
+       ok(packet_header_field,
+               "Packet header structure contains a custom field with the appropriate name");
+       ret_field_type = bt_ctf_field_get_type(packet_header_field);
+       ok(ret_field_type == packet_header_field_type,
+               "Custom packet header field is of the expected type");
+       ok(!bt_ctf_field_unsigned_integer_set_value(packet_header_field,
+               54321), "Set custom packet header value successfully");
+       ok(bt_ctf_stream_set_packet_header(stream1, NULL) < 0,
+               "bt_ctf_stream_set_packet_header handles a NULL packet header correctly");
+       ok(bt_ctf_stream_set_packet_header(NULL, packet_header) < 0,
+               "bt_ctf_stream_set_packet_header handles a NULL stream correctly");
+       ok(bt_ctf_stream_set_packet_header(stream1, packet_header_field) < 0,
+               "bt_ctf_stream_set_packet_header rejects a packet header of the wrong type");
+       ok(!bt_ctf_stream_set_packet_header(stream1, packet_header),
+               "Successfully set a stream's packet header");
+
        append_simple_event(stream_class, stream1, clock);
 
        packet_resize_test(stream_class, stream1, clock);
 
        append_complex_event(stream_class, stream1, clock);
 
+       test_empty_stream(writer);
+
        metadata_string = bt_ctf_writer_get_metadata_string(writer);
        ok(metadata_string, "Get metadata string");
 
@@ -1841,6 +1994,7 @@ int main(int argc, char **argv)
 
        bt_ctf_clock_put(clock);
        bt_ctf_stream_class_put(stream_class);
+       bt_ctf_stream_class_put(ret_stream_class);
        bt_ctf_writer_put(writer);
        bt_ctf_stream_put(stream1);
        bt_ctf_field_type_put(packet_context_type);
@@ -1848,6 +2002,10 @@ int main(int argc, char **argv)
        bt_ctf_field_type_put(integer_type);
        bt_ctf_field_type_put(stream_event_context_type);
        bt_ctf_field_type_put(ret_field_type);
+       bt_ctf_field_type_put(packet_header_type);
+       bt_ctf_field_type_put(packet_header_field_type);
+       bt_ctf_field_put(packet_header);
+       bt_ctf_field_put(packet_header_field);
        bt_ctf_trace_put(trace);
        free(metadata_string);
 
@@ -1867,6 +2025,5 @@ int main(int argc, char **argv)
 
        rmdir(trace_path);
        closedir(trace_dir);
-
        return 0;
 }
This page took 0.037196 seconds and 4 git commands to generate.