Port: replace setenv() by g_setenv()
[babeltrace.git] / tests / lib / test_plugin.c
index 0d11e0ae5b48296082303981b99eacb327e2b4c8..1fb3c6ddbd1c9d1478586712db0c9984eaefa54f 100644 (file)
 #define NON_EXISTING_PATH      "/this/hopefully/does/not/exist/5bc75f8d-0dba-4043-a509-d7984b97e42b.so"
 
 /* Those symbols are written to by some test plugins */
-int test_plugin_init_called;
-int test_plugin_exit_called;
+static int check_env_var(const char *name)
+{
+       const char *val = getenv(name);
+
+       if (!val) {
+               return -1;
+       }
 
-static void reset_test_plugin_symbols(void)
+       return atoi(val);
+}
+
+static void reset_test_plugin_env_vars(void)
 {
-       test_plugin_init_called = 0;
-       test_plugin_exit_called = 0;
+       g_setenv("BT_TEST_PLUGIN_INIT_CALLED", "0", 1);
+       g_setenv("BT_TEST_PLUGIN_EXIT_CALLED", "0", 1);
 }
 
 static char *get_test_plugin_path(const char *plugin_dir,
@@ -102,11 +110,12 @@ static void test_minimal(const char *plugin_dir)
        assert(minimal_path);
        diag("minimal plugin test below");
 
-       reset_test_plugin_symbols();
+       reset_test_plugin_env_vars();
        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(check_env_var("BT_TEST_PLUGIN_INIT_CALLED") == 1,
+               "plugin's initialization function is called during bt_plugin_create_all_from_file()");
        ok(bt_plugin_set_get_plugin_count(plugin_set) == 1,
                "bt_plugin_create_all_from_file() returns the expected number of plugins");
        plugin = bt_plugin_set_get_plugin(plugin_set, 0);
@@ -128,7 +137,8 @@ static void test_minimal(const char *plugin_dir)
                "bt_plugin_get_component_class_count() returns the expected value");
        bt_put(plugin);
        bt_put(plugin_set);
-       ok(test_plugin_exit_called, "plugin's exit function is called when the plugin is destroyed");
+       ok(check_env_var("BT_TEST_PLUGIN_EXIT_CALLED") == 1,
+               "plugin's exit function is called when the plugin is destroyed");
 
        free(minimal_path);
 }
@@ -278,6 +288,7 @@ static void test_create_all_from_dir(const char *plugin_dir)
 
 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;
@@ -287,10 +298,9 @@ 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);
+       assert(ret > 0 && plugin_path);
        g_setenv("BABELTRACE_PLUGIN_PATH", plugin_path, 1);
        plugin = bt_plugin_find("test_minimal");
        ok(plugin,
This page took 0.025226 seconds and 4 git commands to generate.