Test fix: fixed allocation size used for variable length input
[babeltrace.git] / tests / lib / test_plugin.c
index fdc16c5bbdb3b623141f2b853f37c0d5fb3a66de..ad8489e88a27a60ea605886a269168f5eaace91c 100644 (file)
 #include <babeltrace/ref.h>
 #include <babeltrace/values.h>
 #include <babeltrace/graph/component.h>
+#include <babeltrace/graph/graph.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdio.h>
 #include <assert.h>
 #include <glib.h>
-#include <linux/limits.h>
 #include "tap/tap.h"
 #include "common.h"
 
@@ -60,17 +60,17 @@ static char *get_test_plugin_path(const char *plugin_dir,
 
 static void test_invalid(const char *plugin_dir)
 {
-       struct bt_plugin **plugins;
+       struct bt_plugin_set *plugin_set;
 
-       plugins = bt_plugin_create_all_from_file(NON_EXISTING_PATH);
-       ok(!plugins, "bt_plugin_create_all_from_file() fails with a non-existing file");
+       plugin_set = bt_plugin_create_all_from_file(NON_EXISTING_PATH);
+       ok(!plugin_set, "bt_plugin_create_all_from_file() fails with a non-existing file");
 
-       plugins = bt_plugin_create_all_from_file(plugin_dir);
-       ok(!plugins, "bt_plugin_create_all_from_file() fails with a directory");
+       plugin_set = bt_plugin_create_all_from_file(plugin_dir);
+       ok(!plugin_set, "bt_plugin_create_all_from_file() fails with a directory");
 
        ok(!bt_plugin_create_all_from_file(NULL),
                "bt_plugin_create_all_from_file() handles NULL correctly");
-       ok(!bt_plugin_create_all_from_dir(NULL, false),
+       ok(!bt_plugin_create_all_from_dir(NULL, BT_FALSE),
                "bt_plugin_create_all_from_dir() handles NULL correctly");
        ok(!bt_plugin_get_name(NULL),
                "bt_plugin_get_name() handles NULL correctly");
@@ -87,15 +87,15 @@ static void test_invalid(const char *plugin_dir)
                "bt_plugin_get_path() handles NULL correctly");
        ok(bt_plugin_get_component_class_count(NULL) < 0,
                "bt_plugin_get_component_class_count() handles NULL correctly");
-       ok(!bt_plugin_get_component_class(NULL, 0),
-               "bt_plugin_get_component_class() handles NULL correctly");
+       ok(!bt_plugin_get_component_class_by_index(NULL, 0),
+               "bt_plugin_get_component_class_by_index() handles NULL correctly");
        ok(!bt_plugin_get_component_class_by_name_and_type(NULL, NULL, 0),
                "bt_plugin_get_component_class_by_name_and_type() handles NULL correctly");
 }
 
 static void test_minimal(const char *plugin_dir)
 {
-       struct bt_plugin **plugins;
+       struct bt_plugin_set *plugin_set;
        struct bt_plugin *plugin;
        char *minimal_path = get_test_plugin_path(plugin_dir, "minimal");
 
@@ -103,12 +103,13 @@ static void test_minimal(const char *plugin_dir)
        diag("minimal plugin test below");
 
        reset_test_plugin_symbols();
-       plugins = bt_plugin_create_all_from_file(minimal_path);
-       ok(plugins && plugins[0], "bt_plugin_create_all_from_file() succeeds with a valid file");
+       plugin_set = bt_plugin_create_all_from_file(minimal_path);
+       ok(plugin_set && bt_plugin_set_get_plugin_count(plugin_set) == 1,
+               "bt_plugin_create_all_from_file() succeeds with a valid file");
        ok(test_plugin_init_called, "plugin's initialization function is called during bt_plugin_create_all_from_file()");
-       ok(plugins && plugins[0] && !plugins[1],
+       ok(bt_plugin_set_get_plugin_count(plugin_set) == 1,
                "bt_plugin_create_all_from_file() returns the expected number of plugins");
-       plugin = plugins[0];
+       plugin = bt_plugin_set_get_plugin(plugin_set, 0);
        ok(strcmp(bt_plugin_get_name(plugin), "test_minimal") == 0,
                "bt_plugin_get_name() returns the expected name");
        ok(strcmp(bt_plugin_get_description(plugin),
@@ -125,16 +126,16 @@ static void test_minimal(const char *plugin_dir)
                "bt_plugin_get_path() returns the expected path");
        ok(bt_plugin_get_component_class_count(plugin) == 0,
                "bt_plugin_get_component_class_count() returns the expected value");
-       BT_PUT(plugin);
+       bt_put(plugin);
+       bt_put(plugin_set);
        ok(test_plugin_exit_called, "plugin's exit function is called when the plugin is destroyed");
 
        free(minimal_path);
-       free(plugins);
 }
 
 static void test_sfs(const char *plugin_dir)
 {
-       struct bt_plugin **plugins;
+       struct bt_plugin_set *plugin_set;
        struct bt_plugin *plugin;
        struct bt_component_class *sink_comp_class;
        struct bt_component_class *source_comp_class;
@@ -147,15 +148,16 @@ static void test_sfs(const char *plugin_dir)
        struct bt_value *results;
        struct bt_value *object;
        struct bt_value *res_params;
+       struct bt_graph *graph;
        const char *object_str;
        int ret;
 
        assert(sfs_path);
        diag("sfs plugin test below");
 
-       plugins = bt_plugin_create_all_from_file(sfs_path);
-       assert(plugins && plugins[0]);
-       plugin = plugins[0];
+       plugin_set = bt_plugin_create_all_from_file(sfs_path);
+       assert(plugin_set && bt_plugin_set_get_plugin_count(plugin_set) == 1);
+       plugin = bt_plugin_set_get_plugin(plugin_set, 0);
        ok(bt_plugin_get_version(plugin, &major, &minor, &patch, &extra) ==
                BT_PLUGIN_STATUS_OK,
                "bt_plugin_get_version() succeeds when there's a version");
@@ -217,21 +219,36 @@ static void test_sfs(const char *plugin_dir)
 
        diag("> putting the plugin object here");
        BT_PUT(plugin);
-       sink_component = bt_component_create(sink_comp_class, NULL, NULL);
-       ok(sink_component, "bt_component_create() still works after the plugin object is destroyed");
+       graph = bt_graph_create();
+       assert(graph);
+       ret = bt_graph_add_component(graph, sink_comp_class, "the-sink", NULL,
+               &sink_component);
+       ok(ret == 0 && sink_component,
+               "bt_graph_add_component() still works after the plugin object is destroyed");
        BT_PUT(sink_component);
        BT_PUT(source_comp_class);
-       sink_component = bt_component_create(sink_comp_class, NULL, NULL);
-       ok(sink_component, "bt_component_create() still works after the source component class object is destroyed");
+       bt_put(graph);
+       graph = bt_graph_create();
+       assert(graph);
+       ret = bt_graph_add_component(graph, sink_comp_class, "the-sink", NULL,
+               &sink_component);
+       ok(ret == 0 && sink_component,
+               "bt_graph_add_component() still works after the source component class object is destroyed");
        BT_PUT(sink_component);
        BT_PUT(filter_comp_class);
-       sink_component = bt_component_create(sink_comp_class, NULL, NULL);
-       ok(sink_component, "bt_component_create() still works after the filter component class object is destroyed");
+       bt_put(graph);
+       graph = bt_graph_create();
+       assert(graph);
+       ret = bt_graph_add_component(graph, sink_comp_class, "the-sink", NULL,
+               &sink_component);
+       ok(ret == 0 && sink_component,
+               "bt_graph_add_component() still works after the filter component class object is destroyed");
        BT_PUT(sink_comp_class);
        BT_PUT(sink_component);
 
        free(sfs_path);
-       free(plugins);
+       bt_put(graph);
+       bt_put(plugin_set);
        bt_put(object);
        bt_put(res_params);
        bt_put(results);
@@ -240,33 +257,28 @@ static void test_sfs(const char *plugin_dir)
 
 static void test_create_all_from_dir(const char *plugin_dir)
 {
-       struct bt_plugin **plugins;
-       struct bt_plugin *plugin;
-       int i;
+       struct bt_plugin_set *plugin_set;
 
        diag("create from all test below");
 
-       plugins = bt_plugin_create_all_from_dir(NON_EXISTING_PATH, false);
-       ok(!plugins,
+       plugin_set = bt_plugin_create_all_from_dir(NON_EXISTING_PATH, BT_FALSE);
+       ok(!plugin_set,
                "bt_plugin_create_all_from_dir() fails with an invalid path");
 
-       plugins = bt_plugin_create_all_from_dir(plugin_dir, false);
-       ok(plugins, "bt_plugin_create_all_from_dir() succeeds with a valid path");
-
-       i = 0;
-       while ((plugin = plugins[i])) {
-               BT_PUT(plugin);
-               i++;
-       }
+       plugin_set = bt_plugin_create_all_from_dir(plugin_dir, BT_FALSE);
+       ok(plugin_set, "bt_plugin_create_all_from_dir() succeeds with a valid path");
 
        /* 2 or 4, if `.la` files are considered or not */
-       ok(i == 2 || i == 4, "bt_plugin_create_all_from_dir() returns the expected number of plugin objects");
+       ok(bt_plugin_set_get_plugin_count(plugin_set) == 2 ||
+               bt_plugin_set_get_plugin_count(plugin_set) == 4,
+               "bt_plugin_create_all_from_dir() returns the expected number of plugin objects");
 
-       free(plugins);
+       bt_put(plugin_set);
 }
 
 static void test_find(const char *plugin_dir)
 {
+       int ret;
        struct bt_plugin *plugin;
        struct bt_component_class *comp_cls_sink;
        struct bt_component_class *comp_cls_source;
@@ -276,11 +288,10 @@ static void test_find(const char *plugin_dir)
                "bt_plugin_find() handles NULL");
        ok(!bt_plugin_find(NON_EXISTING_PATH),
                "bt_plugin_find() returns NULL with an unknown plugin name");
-       plugin_path = malloc(PATH_MAX * 5);
-       assert(plugin_path);
-       sprintf(plugin_path, "%s:/ec1d09e5-696c-442e-b1c3-f9c6cf7f5958:::%s:8db46494-a398-466a-9649-c765ae077629:",
+       ret = asprintf(&plugin_path, "%s:/ec1d09e5-696c-442e-b1c3-f9c6cf7f5958:::%s:8db46494-a398-466a-9649-c765ae077629:",
                NON_EXISTING_PATH, plugin_dir);
-       setenv("BABELTRACE_PLUGIN_PATH", plugin_path, 1);
+       assert(ret > 0 && plugin_path);
+       g_setenv("BABELTRACE_PLUGIN_PATH", plugin_path, 1);
        plugin = bt_plugin_find("test_minimal");
        ok(plugin,
                "bt_plugin_find() succeeds with a plugin name it can find");
This page took 0.026182 seconds and 4 git commands to generate.