Fix: ctf: notif-iter.c: update_clock() uses the wrong FT sometimes
[babeltrace.git] / tests / lib / test_plugin.c
CommitLineData
cbb9e0b1
PP
1/*
2 * test_plugin.c
3 *
4 * CTF IR Reference Count test
5 *
6 * Copyright (c) 2017 Philippe Proulx <pproulx@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
22#include <babeltrace/plugin/plugin.h>
23#include <babeltrace/ref.h>
24#include <babeltrace/values.h>
cc469c42 25#include <babeltrace/graph/component.h>
cbb9e0b1
PP
26#include <stdlib.h>
27#include <string.h>
28#include <stdio.h>
29#include <assert.h>
30#include <glib.h>
a8b3f23b 31#include <linux/limits.h>
cbb9e0b1
PP
32#include "tap/tap.h"
33#include "common.h"
34
5933c0f2 35#define NR_TESTS 51
cbb9e0b1
PP
36#define NON_EXISTING_PATH "/this/hopefully/does/not/exist/5bc75f8d-0dba-4043-a509-d7984b97e42b.so"
37
38/* Those symbols are written to by some test plugins */
39int test_plugin_init_called;
40int test_plugin_exit_called;
41
42static void reset_test_plugin_symbols(void)
43{
44 test_plugin_init_called = 0;
45 test_plugin_exit_called = 0;
46}
47
48static char *get_test_plugin_path(const char *plugin_dir,
49 const char *plugin_name)
50{
51 GString *path = g_string_new(plugin_dir);
52 char *ret;
53
54 assert(path);
55 g_string_append_printf(path, "/plugin-%s.so", plugin_name);
56 ret = path->str;
57 g_string_free(path, FALSE);
58 return ret;
59}
60
61static void test_invalid(const char *plugin_dir)
62{
6ba0b073 63 struct bt_plugin **plugins;
cbb9e0b1 64
6ba0b073
PP
65 plugins = bt_plugin_create_all_from_file(NON_EXISTING_PATH);
66 ok(!plugins, "bt_plugin_create_all_from_file() fails with a non-existing file");
cbb9e0b1 67
6ba0b073
PP
68 plugins = bt_plugin_create_all_from_file(plugin_dir);
69 ok(!plugins, "bt_plugin_create_all_from_file() fails with a directory");
cbb9e0b1 70
6ba0b073
PP
71 ok(!bt_plugin_create_all_from_file(NULL),
72 "bt_plugin_create_all_from_file() handles NULL correctly");
cbb9e0b1
PP
73 ok(!bt_plugin_create_all_from_dir(NULL, false),
74 "bt_plugin_create_all_from_dir() handles NULL correctly");
75 ok(!bt_plugin_get_name(NULL),
76 "bt_plugin_get_name() handles NULL correctly");
77 ok(!bt_plugin_get_description(NULL),
78 "bt_plugin_get_description() handles NULL correctly");
458e8e1d
PP
79 ok(bt_plugin_get_version(NULL, NULL, NULL, NULL, NULL) !=
80 BT_PLUGIN_STATUS_OK,
81 "bt_plugin_get_version() handles NULL correctly");
cbb9e0b1
PP
82 ok(!bt_plugin_get_author(NULL),
83 "bt_plugin_get_author() handles NULL correctly");
84 ok(!bt_plugin_get_license(NULL),
85 "bt_plugin_get_license() handles NULL correctly");
86 ok(!bt_plugin_get_path(NULL),
87 "bt_plugin_get_path() handles NULL correctly");
88 ok(bt_plugin_get_component_class_count(NULL) < 0,
89 "bt_plugin_get_component_class_count() handles NULL correctly");
90 ok(!bt_plugin_get_component_class(NULL, 0),
91 "bt_plugin_get_component_class() handles NULL correctly");
92 ok(!bt_plugin_get_component_class_by_name_and_type(NULL, NULL, 0),
93 "bt_plugin_get_component_class_by_name_and_type() handles NULL correctly");
cbb9e0b1
PP
94}
95
96static void test_minimal(const char *plugin_dir)
97{
6ba0b073 98 struct bt_plugin **plugins;
cbb9e0b1
PP
99 struct bt_plugin *plugin;
100 char *minimal_path = get_test_plugin_path(plugin_dir, "minimal");
101
102 assert(minimal_path);
103 diag("minimal plugin test below");
104
105 reset_test_plugin_symbols();
6ba0b073
PP
106 plugins = bt_plugin_create_all_from_file(minimal_path);
107 ok(plugins && plugins[0], "bt_plugin_create_all_from_file() succeeds with a valid file");
108 ok(test_plugin_init_called, "plugin's initialization function is called during bt_plugin_create_all_from_file()");
109 ok(plugins && plugins[0] && !plugins[1],
110 "bt_plugin_create_all_from_file() returns the expected number of plugins");
111 plugin = plugins[0];
112 ok(strcmp(bt_plugin_get_name(plugin), "test_minimal") == 0,
cbb9e0b1
PP
113 "bt_plugin_get_name() returns the expected name");
114 ok(strcmp(bt_plugin_get_description(plugin),
115 "Minimal Babeltrace plugin with no component classes") == 0,
116 "bt_plugin_get_description() returns the expected description");
458e8e1d
PP
117 ok(bt_plugin_get_version(plugin, NULL, NULL, NULL, NULL) !=
118 BT_PLUGIN_STATUS_OK,
119 "bt_plugin_get_version() fails when there's no version");
cbb9e0b1
PP
120 ok(strcmp(bt_plugin_get_author(plugin), "Janine Sutto") == 0,
121 "bt_plugin_get_author() returns the expected author");
122 ok(strcmp(bt_plugin_get_license(plugin), "Beerware") == 0,
123 "bt_plugin_get_license() returns the expected license");
124 ok(strcmp(bt_plugin_get_path(plugin), minimal_path) == 0,
125 "bt_plugin_get_path() returns the expected path");
126 ok(bt_plugin_get_component_class_count(plugin) == 0,
127 "bt_plugin_get_component_class_count() returns the expected value");
128 BT_PUT(plugin);
129 ok(test_plugin_exit_called, "plugin's exit function is called when the plugin is destroyed");
130
131 free(minimal_path);
6ba0b073 132 free(plugins);
cbb9e0b1
PP
133}
134
135static void test_sfs(const char *plugin_dir)
136{
6ba0b073 137 struct bt_plugin **plugins;
cbb9e0b1
PP
138 struct bt_plugin *plugin;
139 struct bt_component_class *sink_comp_class;
140 struct bt_component_class *source_comp_class;
141 struct bt_component_class *filter_comp_class;
142 struct bt_component *sink_component;
143 char *sfs_path = get_test_plugin_path(plugin_dir, "sfs");
458e8e1d
PP
144 unsigned int major, minor, patch;
145 const char *extra;
5933c0f2
PP
146 struct bt_value *params;
147 struct bt_value *results;
a67681c1 148 struct bt_value *object;
5933c0f2 149 struct bt_value *res_params;
a67681c1 150 const char *object_str;
5933c0f2 151 int ret;
cbb9e0b1
PP
152
153 assert(sfs_path);
154 diag("sfs plugin test below");
155
6ba0b073
PP
156 plugins = bt_plugin_create_all_from_file(sfs_path);
157 assert(plugins && plugins[0]);
158 plugin = plugins[0];
458e8e1d
PP
159 ok(bt_plugin_get_version(plugin, &major, &minor, &patch, &extra) ==
160 BT_PLUGIN_STATUS_OK,
161 "bt_plugin_get_version() succeeds when there's a version");
162 ok(major == 1,
163 "bt_plugin_get_version() returns the expected major version");
164 ok(minor == 2,
165 "bt_plugin_get_version() returns the expected minor version");
166 ok(patch == 3,
167 "bt_plugin_get_version() returns the expected patch version");
168 ok(strcmp(extra, "yes") == 0,
169 "bt_plugin_get_version() returns the expected extra version");
cbb9e0b1
PP
170 ok(bt_plugin_get_component_class_count(plugin) == 3,
171 "bt_plugin_get_component_class_count() returns the expected value");
172
173 source_comp_class = bt_plugin_get_component_class_by_name_and_type(
d3e4dcd8 174 plugin, "source", BT_COMPONENT_CLASS_TYPE_SOURCE);
cbb9e0b1
PP
175 ok(source_comp_class,
176 "bt_plugin_get_component_class_by_name_and_type() finds a source component class");
177
178 sink_comp_class = bt_plugin_get_component_class_by_name_and_type(
d3e4dcd8 179 plugin, "sink", BT_COMPONENT_CLASS_TYPE_SINK);
cbb9e0b1
PP
180 ok(sink_comp_class,
181 "bt_plugin_get_component_class_by_name_and_type() finds a sink component class");
a889b89f
PP
182 ok(strcmp(bt_component_class_get_help(sink_comp_class),
183 "Bacon ipsum dolor amet strip steak cupim pastrami venison shoulder.\n"
184 "Prosciutto beef ribs flank meatloaf pancetta brisket kielbasa drumstick\n"
185 "venison tenderloin cow tail. Beef short loin shoulder meatball, sirloin\n"
186 "ground round brisket salami cupim pork bresaola turkey bacon boudin.\n") == 0,
187 "bt_component_class_get_help() returns the expected help text");
188
cbb9e0b1 189 filter_comp_class = bt_plugin_get_component_class_by_name_and_type(
d3e4dcd8 190 plugin, "filter", BT_COMPONENT_CLASS_TYPE_FILTER);
cbb9e0b1
PP
191 ok(filter_comp_class,
192 "bt_plugin_get_component_class_by_name_and_type() finds a filter component class");
193 ok(!bt_plugin_get_component_class_by_name_and_type(plugin, "filter",
d3e4dcd8 194 BT_COMPONENT_CLASS_TYPE_SOURCE),
cbb9e0b1 195 "bt_plugin_get_component_class_by_name_and_type() does not find a component class given the wrong type");
5933c0f2
PP
196 params = bt_value_integer_create_init(23);
197 assert(params);
a67681c1
PP
198 ok (!bt_component_class_query(NULL, "get-something", params),
199 "bt_component_class_query() handles NULL (component class)");
200 ok (!bt_component_class_query(filter_comp_class, NULL, params),
201 "bt_component_class_query() handles NULL (object)");
202 ok (!bt_component_class_query(filter_comp_class, "get-something", NULL),
203 "bt_component_class_query() handles NULL (parameters)");
204 results = bt_component_class_query(filter_comp_class,
5933c0f2 205 "get-something", params);
a67681c1 206 ok(results, "bt_component_class_query() succeeds");
5933c0f2 207 assert(bt_value_is_array(results) && bt_value_array_size(results) == 2);
a67681c1
PP
208 object = bt_value_array_get(results, 0);
209 assert(object && bt_value_is_string(object));
210 ret = bt_value_string_get(object, &object_str);
5933c0f2 211 assert(ret == 0);
a67681c1
PP
212 ok(strcmp(object_str, "get-something") == 0,
213 "bt_component_class_query() receives the expected object name");
5933c0f2
PP
214 res_params = bt_value_array_get(results, 1);
215 ok(res_params == params,
a67681c1 216 "bt_component_class_query() receives the expected parameters");
cbb9e0b1
PP
217
218 diag("> putting the plugin object here");
219 BT_PUT(plugin);
220 sink_component = bt_component_create(sink_comp_class, NULL, bt_value_null);
221 ok(sink_component, "bt_component_create() still works after the plugin object is destroyed");
222 BT_PUT(sink_component);
223 BT_PUT(source_comp_class);
224 sink_component = bt_component_create(sink_comp_class, NULL, bt_value_null);
225 ok(sink_component, "bt_component_create() still works after the source component class object is destroyed");
226 BT_PUT(sink_component);
227 BT_PUT(filter_comp_class);
228 sink_component = bt_component_create(sink_comp_class, NULL, bt_value_null);
229 ok(sink_component, "bt_component_create() still works after the filter component class object is destroyed");
230 BT_PUT(sink_comp_class);
231 BT_PUT(sink_component);
232
233 free(sfs_path);
6ba0b073 234 free(plugins);
a67681c1 235 bt_put(object);
5933c0f2
PP
236 bt_put(res_params);
237 bt_put(results);
238 bt_put(params);
cbb9e0b1
PP
239}
240
241static void test_create_all_from_dir(const char *plugin_dir)
242{
243 struct bt_plugin **plugins;
244 struct bt_plugin *plugin;
245 int i;
246
247 diag("create from all test below");
248
249 plugins = bt_plugin_create_all_from_dir(NON_EXISTING_PATH, false);
250 ok(!plugins,
251 "bt_plugin_create_all_from_dir() fails with an invalid path");
252
253 plugins = bt_plugin_create_all_from_dir(plugin_dir, false);
254 ok(plugins, "bt_plugin_create_all_from_dir() succeeds with a valid path");
255
256 i = 0;
257 while ((plugin = plugins[i])) {
258 BT_PUT(plugin);
259 i++;
260 }
261
262 /* 2 or 4, if `.la` files are considered or not */
263 ok(i == 2 || i == 4, "bt_plugin_create_all_from_dir() returns the expected number of plugin objects");
264
265 free(plugins);
266}
267
a8b3f23b
PP
268static void test_create_from_name(const char *plugin_dir)
269{
270 struct bt_plugin *plugin;
271 char *plugin_path;
272
273 ok(!bt_plugin_create_from_name(NULL),
274 "bt_plugin_create_from_name() handles NULL");
275 ok(!bt_plugin_create_from_name(NON_EXISTING_PATH),
276 "bt_plugin_create_from_name() returns NULL with an unknown plugin name");
277 plugin_path = malloc(PATH_MAX * 5);
278 assert(plugin_path);
279 sprintf(plugin_path, "%s:/ec1d09e5-696c-442e-b1c3-f9c6cf7f5958:::%s:8db46494-a398-466a-9649-c765ae077629:",
280 NON_EXISTING_PATH, plugin_dir);
281 setenv("BABELTRACE_PLUGIN_PATH", plugin_path, 1);
282 plugin = bt_plugin_create_from_name("test_minimal");
283 ok(plugin,
284 "bt_plugin_create_from_name() succeeds with a plugin name it can find");
285 ok(strcmp(bt_plugin_get_author(plugin), "Janine Sutto") == 0,
286 "bt_plugin_create_from_name() finds the correct plugin for a given name");
287 BT_PUT(plugin);
288 free(plugin_path);
289}
290
cbb9e0b1
PP
291int main(int argc, char **argv)
292{
293 int ret;
294 const char *plugin_dir;
295
296 if (argc != 2) {
297 puts("Usage: test_plugin plugin_directory");
298 ret = 1;
299 goto end;
300 }
301
302 plugin_dir = argv[1];
303 plan_tests(NR_TESTS);
304 test_invalid(plugin_dir);
305 test_minimal(plugin_dir);
306 test_sfs(plugin_dir);
307 test_create_all_from_dir(plugin_dir);
a8b3f23b 308 test_create_from_name(plugin_dir);
cbb9e0b1
PP
309 ret = exit_status();
310end:
311 return ret;
312}
This page took 0.049231 seconds and 4 git commands to generate.