lib: graph: add "self" and some "private" APIs
[babeltrace.git] / tests / lib / test_plugin.c
1 /*
2 * Copyright (c) 2017 Philippe Proulx <pproulx@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; under version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #include <babeltrace/babeltrace.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <babeltrace/assert-internal.h>
23 #include <glib.h>
24 #include "tap/tap.h"
25 #include "common.h"
26
27 #define NR_TESTS 35
28 #define NON_EXISTING_PATH "/this/hopefully/does/not/exist/5bc75f8d-0dba-4043-a509-d7984b97e42b.so"
29
30 /* Those symbols are written to by some test plugins */
31 static int check_env_var(const char *name)
32 {
33 const char *val = getenv(name);
34
35 if (!val) {
36 return -1;
37 }
38
39 return atoi(val);
40 }
41
42 static void reset_test_plugin_env_vars(void)
43 {
44 g_setenv("BT_TEST_PLUGIN_INIT_CALLED", "0", 1);
45 g_setenv("BT_TEST_PLUGIN_EXIT_CALLED", "0", 1);
46 }
47
48 static char *get_test_plugin_path(const char *plugin_dir,
49 const char *plugin_name)
50 {
51 char *ret;
52 char *plugin_file_name;
53
54 if (asprintf(&plugin_file_name, "plugin-%s." G_MODULE_SUFFIX,
55 plugin_name) == -1) {
56 abort();
57 }
58
59 ret = g_build_filename(plugin_dir, plugin_file_name, NULL);
60 free(plugin_file_name);
61
62 return ret;
63 }
64
65 static void test_minimal(const char *plugin_dir)
66 {
67 struct bt_plugin_set *plugin_set;
68 struct bt_plugin *plugin;
69 char *minimal_path = get_test_plugin_path(plugin_dir, "minimal");
70
71 BT_ASSERT(minimal_path);
72 diag("minimal plugin test below");
73
74 reset_test_plugin_env_vars();
75 plugin_set = bt_plugin_create_all_from_file(minimal_path);
76 ok(plugin_set && bt_plugin_set_get_plugin_count(plugin_set) == 1,
77 "bt_plugin_create_all_from_file() succeeds with a valid file");
78 ok(check_env_var("BT_TEST_PLUGIN_INIT_CALLED") == 1,
79 "plugin's initialization function is called during bt_plugin_create_all_from_file()");
80 ok(bt_plugin_set_get_plugin_count(plugin_set) == 1,
81 "bt_plugin_create_all_from_file() returns the expected number of plugins");
82 plugin = bt_plugin_set_borrow_plugin_by_index(plugin_set, 0);
83 ok(strcmp(bt_plugin_get_name(plugin), "test_minimal") == 0,
84 "bt_plugin_get_name() returns the expected name");
85 ok(strcmp(bt_plugin_get_description(plugin),
86 "Minimal Babeltrace plugin with no component classes") == 0,
87 "bt_plugin_get_description() returns the expected description");
88 ok(bt_plugin_get_version(plugin, NULL, NULL, NULL, NULL) ==
89 BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE,
90 "bt_plugin_get_version() fails when there's no version");
91 ok(strcmp(bt_plugin_get_author(plugin), "Janine Sutto") == 0,
92 "bt_plugin_get_author() returns the expected author");
93 ok(strcmp(bt_plugin_get_license(plugin), "Beerware") == 0,
94 "bt_plugin_get_license() returns the expected license");
95 ok(strcmp(bt_plugin_get_path(plugin), minimal_path) == 0,
96 "bt_plugin_get_path() returns the expected path");
97 ok(bt_plugin_get_source_component_class_count(plugin) == 0,
98 "bt_plugin_get_source_component_class_count() returns the expected value");
99 ok(bt_plugin_get_filter_component_class_count(plugin) == 0,
100 "bt_plugin_get_filter_component_class_count() returns the expected value");
101 ok(bt_plugin_get_sink_component_class_count(plugin) == 0,
102 "bt_plugin_get_sink_component_class_count() returns the expected value");
103 bt_object_put_ref(plugin_set);
104 ok(check_env_var("BT_TEST_PLUGIN_EXIT_CALLED") == 1,
105 "plugin's exit function is called when the plugin is destroyed");
106
107 free(minimal_path);
108 }
109
110 static void test_sfs(const char *plugin_dir)
111 {
112 struct bt_plugin_set *plugin_set;
113 struct bt_plugin *plugin;
114 struct bt_component_class_sink *sink_comp_class;
115 struct bt_component_class_source *source_comp_class;
116 struct bt_component_class_filter *filter_comp_class;
117 struct bt_component_sink *sink_component;
118 char *sfs_path = get_test_plugin_path(plugin_dir, "sfs");
119 unsigned int major, minor, patch;
120 const char *extra;
121 struct bt_private_value *params;
122 struct bt_value *results;
123 struct bt_value *object;
124 struct bt_value *res_params;
125 struct bt_private_graph *graph;
126 const char *object_str;
127 enum bt_graph_status graph_ret;
128 struct bt_private_query_executor *query_exec =
129 bt_private_query_executor_create();
130 int ret;
131
132 BT_ASSERT(query_exec);
133 BT_ASSERT(sfs_path);
134 diag("sfs plugin test below");
135
136 plugin_set = bt_plugin_create_all_from_file(sfs_path);
137 BT_ASSERT(plugin_set && bt_plugin_set_get_plugin_count(plugin_set) == 1);
138 plugin = bt_plugin_set_borrow_plugin_by_index(plugin_set, 0);
139 ok(bt_plugin_get_version(plugin, &major, &minor, &patch, &extra) ==
140 BT_PROPERTY_AVAILABILITY_AVAILABLE,
141 "bt_plugin_get_version() succeeds when there's a version");
142 ok(major == 1,
143 "bt_plugin_get_version() returns the expected major version");
144 ok(minor == 2,
145 "bt_plugin_get_version() returns the expected minor version");
146 ok(patch == 3,
147 "bt_plugin_get_version() returns the expected patch version");
148 ok(strcmp(extra, "yes") == 0,
149 "bt_plugin_get_version() returns the expected extra version");
150 ok(bt_plugin_get_source_component_class_count(plugin) == 1,
151 "bt_plugin_get_source_component_class_count() returns the expected value");
152 ok(bt_plugin_get_filter_component_class_count(plugin) == 1,
153 "bt_plugin_get_filter_component_class_count() returns the expected value");
154 ok(bt_plugin_get_sink_component_class_count(plugin) == 1,
155 "bt_plugin_get_sink_component_class_count() returns the expected value");
156
157 source_comp_class = bt_plugin_borrow_source_component_class_by_name(
158 plugin, "source");
159 ok(source_comp_class,
160 "bt_plugin_borrow_source_component_class_by_name() finds a source component class");
161
162 sink_comp_class = bt_plugin_borrow_sink_component_class_by_name(
163 plugin, "sink");
164 ok(sink_comp_class,
165 "bt_plugin_borrow_sink_component_class_by_name() finds a sink component class");
166 ok(strcmp(bt_component_class_get_help(
167 bt_component_class_sink_borrow_component_class(sink_comp_class)),
168 "Bacon ipsum dolor amet strip steak cupim pastrami venison shoulder.\n"
169 "Prosciutto beef ribs flank meatloaf pancetta brisket kielbasa drumstick\n"
170 "venison tenderloin cow tail. Beef short loin shoulder meatball, sirloin\n"
171 "ground round brisket salami cupim pork bresaola turkey bacon boudin.\n") == 0,
172 "bt_component_class_get_help() returns the expected help text");
173
174 filter_comp_class = bt_plugin_borrow_filter_component_class_by_name(
175 plugin, "filter");
176 ok(filter_comp_class,
177 "bt_plugin_borrow_filter_component_class_by_name() finds a filter component class");
178 params = bt_private_value_integer_create_init(23);
179 BT_ASSERT(params);
180 ret = bt_private_query_executor_query(query_exec,
181 bt_component_class_filter_borrow_component_class(filter_comp_class),
182 "get-something", bt_private_value_borrow_value(params),
183 &results);
184 ok(ret == 0 && results, "bt_private_query_executor_query() succeeds");
185 BT_ASSERT(bt_value_is_array(results) && bt_value_array_get_size(results) == 2);
186 object = bt_value_array_borrow_element_by_index(results, 0);
187 BT_ASSERT(object && bt_value_is_string(object));
188 object_str = bt_value_string_get(object);
189 ok(strcmp(object_str, "get-something") == 0,
190 "bt_component_class_query() receives the expected object name");
191 res_params = bt_value_array_borrow_element_by_index(results, 1);
192 ok(res_params == bt_private_value_borrow_value(params),
193 "bt_component_class_query() receives the expected parameters");
194
195 bt_object_get_ref(sink_comp_class);
196 diag("> putting the plugin set object here");
197 BT_OBJECT_PUT_REF_AND_RESET(plugin_set);
198 graph = bt_private_graph_create();
199 BT_ASSERT(graph);
200 graph_ret = bt_private_graph_add_sink_component(graph, sink_comp_class, "the-sink",
201 NULL, &sink_component);
202 ok(graph_ret == BT_GRAPH_STATUS_OK && sink_component,
203 "bt_private_graph_add_sink_component() still works after the plugin object is destroyed");
204 BT_OBJECT_PUT_REF_AND_RESET(sink_component);
205 bt_object_put_ref(graph);
206
207 free(sfs_path);
208 bt_object_put_ref(sink_comp_class);
209 bt_object_put_ref(results);
210 bt_object_put_ref(params);
211 bt_object_put_ref(query_exec);
212 }
213
214 static void test_create_all_from_dir(const char *plugin_dir)
215 {
216 struct bt_plugin_set *plugin_set;
217
218 diag("create from all test below");
219
220 plugin_set = bt_plugin_create_all_from_dir(NON_EXISTING_PATH, BT_FALSE);
221 ok(!plugin_set,
222 "bt_plugin_create_all_from_dir() fails with an invalid path");
223
224 plugin_set = bt_plugin_create_all_from_dir(plugin_dir, BT_FALSE);
225 ok(plugin_set, "bt_plugin_create_all_from_dir() succeeds with a valid path");
226
227 /* 2 or 4, if `.la` files are considered or not */
228 ok(bt_plugin_set_get_plugin_count(plugin_set) == 2 ||
229 bt_plugin_set_get_plugin_count(plugin_set) == 4,
230 "bt_plugin_create_all_from_dir() returns the expected number of plugin objects");
231
232 bt_object_put_ref(plugin_set);
233 }
234
235 static void test_find(const char *plugin_dir)
236 {
237 int ret;
238 struct bt_plugin *plugin;
239 char *plugin_path;
240
241 ok(!bt_plugin_find(NON_EXISTING_PATH),
242 "bt_plugin_find() returns NULL with an unknown plugin name");
243 ret = asprintf(&plugin_path, "%s" G_SEARCHPATH_SEPARATOR_S
244 G_DIR_SEPARATOR_S "ec1d09e5-696c-442e-b1c3-f9c6cf7f5958"
245 G_SEARCHPATH_SEPARATOR_S G_SEARCHPATH_SEPARATOR_S
246 G_SEARCHPATH_SEPARATOR_S "%s" G_SEARCHPATH_SEPARATOR_S
247 "8db46494-a398-466a-9649-c765ae077629"
248 G_SEARCHPATH_SEPARATOR_S,
249 NON_EXISTING_PATH, plugin_dir);
250 BT_ASSERT(ret > 0 && plugin_path);
251 g_setenv("BABELTRACE_PLUGIN_PATH", plugin_path, 1);
252 plugin = bt_plugin_find("test_minimal");
253 ok(plugin,
254 "bt_plugin_find() succeeds with a plugin name it can find");
255 ok(strcmp(bt_plugin_get_author(plugin), "Janine Sutto") == 0,
256 "bt_plugin_find() finds the correct plugin for a given name");
257 BT_OBJECT_PUT_REF_AND_RESET(plugin);
258 free(plugin_path);
259 }
260
261 int main(int argc, char **argv)
262 {
263 int ret;
264 const char *plugin_dir;
265
266 if (argc != 2) {
267 puts("Usage: test_plugin plugin_directory");
268 ret = 1;
269 goto end;
270 }
271
272 plugin_dir = argv[1];
273 plan_tests(NR_TESTS);
274 test_minimal(plugin_dir);
275 test_sfs(plugin_dir);
276 test_create_all_from_dir(plugin_dir);
277 test_find(plugin_dir);
278 ret = exit_status();
279 end:
280 return ret;
281 }
This page took 0.03988 seconds and 4 git commands to generate.