Commit | Line | Data |
---|---|---|
cbb9e0b1 | 1 | /* |
0235b0db | 2 | * SPDX-License-Identifier: GPL-2.0-only |
cbb9e0b1 | 3 | * |
0235b0db | 4 | * Copyright (C) 2017 Philippe Proulx <pproulx@efficios.com> |
cbb9e0b1 PP |
5 | */ |
6 | ||
3fadfbc0 | 7 | #include <babeltrace2/babeltrace.h> |
cbb9e0b1 PP |
8 | #include <stdlib.h> |
9 | #include <string.h> | |
10 | #include <stdio.h> | |
578e048b | 11 | #include "common/assert.h" |
cbb9e0b1 PP |
12 | #include <glib.h> |
13 | #include "tap/tap.h" | |
14 | #include "common.h" | |
15 | ||
9736d991 | 16 | #define NR_TESTS 38 |
cbb9e0b1 PP |
17 | #define NON_EXISTING_PATH "/this/hopefully/does/not/exist/5bc75f8d-0dba-4043-a509-d7984b97e42b.so" |
18 | ||
19 | /* Those symbols are written to by some test plugins */ | |
df5b5d01 PP |
20 | static int check_env_var(const char *name) |
21 | { | |
22 | const char *val = getenv(name); | |
23 | ||
24 | if (!val) { | |
25 | return -1; | |
26 | } | |
27 | ||
28 | return atoi(val); | |
29 | } | |
cbb9e0b1 | 30 | |
df5b5d01 | 31 | static void reset_test_plugin_env_vars(void) |
cbb9e0b1 | 32 | { |
21a9f056 | 33 | g_setenv("BT_TEST_PLUGIN_INITIALIZE_CALLED", "0", 1); |
a71c9a60 | 34 | g_setenv("BT_TEST_PLUGIN_FINALIZE_CALLED", "0", 1); |
cbb9e0b1 PP |
35 | } |
36 | ||
37 | static char *get_test_plugin_path(const char *plugin_dir, | |
38 | const char *plugin_name) | |
39 | { | |
cbb9e0b1 | 40 | char *ret; |
84095ea4 MJ |
41 | char *plugin_file_name; |
42 | ||
43 | if (asprintf(&plugin_file_name, "plugin-%s." G_MODULE_SUFFIX, | |
44 | plugin_name) == -1) { | |
45 | abort(); | |
46 | } | |
47 | ||
48 | ret = g_build_filename(plugin_dir, plugin_file_name, NULL); | |
49 | free(plugin_file_name); | |
cbb9e0b1 | 50 | |
cbb9e0b1 PP |
51 | return ret; |
52 | } | |
53 | ||
cbb9e0b1 PP |
54 | static void test_minimal(const char *plugin_dir) |
55 | { | |
9736d991 | 56 | const bt_plugin_set *plugin_set = NULL; |
b19ff26f | 57 | const bt_plugin *plugin; |
cbb9e0b1 | 58 | char *minimal_path = get_test_plugin_path(plugin_dir, "minimal"); |
d24d5663 | 59 | bt_plugin_find_all_from_file_status status; |
cbb9e0b1 | 60 | |
25583cd0 | 61 | BT_ASSERT(minimal_path); |
cbb9e0b1 PP |
62 | diag("minimal plugin test below"); |
63 | ||
df5b5d01 | 64 | reset_test_plugin_env_vars(); |
9736d991 PP |
65 | status = bt_plugin_find_all_from_file(minimal_path, BT_FALSE, |
66 | &plugin_set); | |
d24d5663 | 67 | ok(status == BT_PLUGIN_FIND_ALL_FROM_FILE_STATUS_OK, |
c8db3219 | 68 | "bt_plugin_find_all_from_file() succeeds with a valid file"); |
9736d991 PP |
69 | ok(plugin_set, |
70 | "bt_plugin_find_all_from_file() returns a plugin set"); | |
21a9f056 | 71 | ok(check_env_var("BT_TEST_PLUGIN_INITIALIZE_CALLED") == 1, |
c8db3219 | 72 | "plugin's initialization function is called during bt_plugin_find_all_from_file()"); |
a8ff38ef | 73 | ok(bt_plugin_set_get_plugin_count(plugin_set) == 1, |
c8db3219 | 74 | "bt_plugin_find_all_from_file() returns the expected number of plugins"); |
92fed4e1 | 75 | plugin = bt_plugin_set_borrow_plugin_by_index_const(plugin_set, 0); |
6ba0b073 | 76 | ok(strcmp(bt_plugin_get_name(plugin), "test_minimal") == 0, |
cbb9e0b1 PP |
77 | "bt_plugin_get_name() returns the expected name"); |
78 | ok(strcmp(bt_plugin_get_description(plugin), | |
79 | "Minimal Babeltrace plugin with no component classes") == 0, | |
80 | "bt_plugin_get_description() returns the expected description"); | |
d94d92ac PP |
81 | ok(bt_plugin_get_version(plugin, NULL, NULL, NULL, NULL) == |
82 | BT_PROPERTY_AVAILABILITY_NOT_AVAILABLE, | |
458e8e1d | 83 | "bt_plugin_get_version() fails when there's no version"); |
cbb9e0b1 PP |
84 | ok(strcmp(bt_plugin_get_author(plugin), "Janine Sutto") == 0, |
85 | "bt_plugin_get_author() returns the expected author"); | |
86 | ok(strcmp(bt_plugin_get_license(plugin), "Beerware") == 0, | |
87 | "bt_plugin_get_license() returns the expected license"); | |
88 | ok(strcmp(bt_plugin_get_path(plugin), minimal_path) == 0, | |
89 | "bt_plugin_get_path() returns the expected path"); | |
d94d92ac PP |
90 | ok(bt_plugin_get_source_component_class_count(plugin) == 0, |
91 | "bt_plugin_get_source_component_class_count() returns the expected value"); | |
92 | ok(bt_plugin_get_filter_component_class_count(plugin) == 0, | |
93 | "bt_plugin_get_filter_component_class_count() returns the expected value"); | |
94 | ok(bt_plugin_get_sink_component_class_count(plugin) == 0, | |
95 | "bt_plugin_get_sink_component_class_count() returns the expected value"); | |
c5b9b441 | 96 | bt_plugin_set_put_ref(plugin_set); |
a71c9a60 FD |
97 | ok(check_env_var("BT_TEST_PLUGIN_FINALIZE_CALLED") == 1, |
98 | "plugin's finalize function is called when the plugin is destroyed"); | |
cbb9e0b1 PP |
99 | |
100 | free(minimal_path); | |
101 | } | |
102 | ||
103 | static void test_sfs(const char *plugin_dir) | |
104 | { | |
9736d991 | 105 | const bt_plugin_set *plugin_set = NULL; |
b19ff26f PP |
106 | const bt_plugin *plugin; |
107 | const bt_component_class_sink *sink_comp_class; | |
108 | const bt_component_class_source *source_comp_class; | |
109 | const bt_component_class_filter *filter_comp_class; | |
110 | const bt_component_sink *sink_component; | |
cbb9e0b1 | 111 | char *sfs_path = get_test_plugin_path(plugin_dir, "sfs"); |
458e8e1d PP |
112 | unsigned int major, minor, patch; |
113 | const char *extra; | |
b19ff26f PP |
114 | bt_value *params; |
115 | const bt_value *results; | |
116 | const bt_value *object; | |
117 | const bt_value *res_params; | |
118 | bt_graph *graph; | |
a67681c1 | 119 | const char *object_str; |
d24d5663 | 120 | bt_graph_add_component_status graph_ret; |
3c729b9a | 121 | bt_query_executor *query_exec; |
c7eee084 | 122 | int ret; |
d24d5663 | 123 | bt_plugin_find_all_from_file_status status; |
cbb9e0b1 | 124 | |
25583cd0 | 125 | BT_ASSERT(sfs_path); |
cbb9e0b1 PP |
126 | diag("sfs plugin test below"); |
127 | ||
9736d991 | 128 | status = bt_plugin_find_all_from_file(sfs_path, BT_FALSE, &plugin_set); |
d24d5663 PP |
129 | BT_ASSERT(status == BT_PLUGIN_FIND_ALL_FROM_FILE_STATUS_OK && |
130 | plugin_set && bt_plugin_set_get_plugin_count(plugin_set) == 1); | |
92fed4e1 | 131 | plugin = bt_plugin_set_borrow_plugin_by_index_const(plugin_set, 0); |
458e8e1d | 132 | ok(bt_plugin_get_version(plugin, &major, &minor, &patch, &extra) == |
d94d92ac | 133 | BT_PROPERTY_AVAILABILITY_AVAILABLE, |
458e8e1d PP |
134 | "bt_plugin_get_version() succeeds when there's a version"); |
135 | ok(major == 1, | |
136 | "bt_plugin_get_version() returns the expected major version"); | |
137 | ok(minor == 2, | |
138 | "bt_plugin_get_version() returns the expected minor version"); | |
139 | ok(patch == 3, | |
140 | "bt_plugin_get_version() returns the expected patch version"); | |
141 | ok(strcmp(extra, "yes") == 0, | |
142 | "bt_plugin_get_version() returns the expected extra version"); | |
d94d92ac PP |
143 | ok(bt_plugin_get_source_component_class_count(plugin) == 1, |
144 | "bt_plugin_get_source_component_class_count() returns the expected value"); | |
145 | ok(bt_plugin_get_filter_component_class_count(plugin) == 1, | |
146 | "bt_plugin_get_filter_component_class_count() returns the expected value"); | |
147 | ok(bt_plugin_get_sink_component_class_count(plugin) == 1, | |
148 | "bt_plugin_get_sink_component_class_count() returns the expected value"); | |
149 | ||
92fed4e1 | 150 | source_comp_class = bt_plugin_borrow_source_component_class_by_name_const( |
d94d92ac | 151 | plugin, "source"); |
cbb9e0b1 | 152 | ok(source_comp_class, |
92fed4e1 | 153 | "bt_plugin_borrow_source_component_class_by_name_const() finds a source component class"); |
cbb9e0b1 | 154 | |
92fed4e1 | 155 | sink_comp_class = bt_plugin_borrow_sink_component_class_by_name_const( |
d94d92ac | 156 | plugin, "sink"); |
cbb9e0b1 | 157 | ok(sink_comp_class, |
92fed4e1 | 158 | "bt_plugin_borrow_sink_component_class_by_name_const() finds a sink component class"); |
0d72b8c3 PP |
159 | ok(strcmp(bt_component_class_get_help(bt_component_class_sink_as_component_class_const(sink_comp_class)), |
160 | "Bacon ipsum dolor amet strip steak cupim pastrami venison shoulder.\n" | |
161 | "Prosciutto beef ribs flank meatloaf pancetta brisket kielbasa drumstick\n" | |
162 | "venison tenderloin cow tail. Beef short loin shoulder meatball, sirloin\n" | |
163 | "ground round brisket salami cupim pork bresaola turkey bacon boudin.\n") == 0, | |
a889b89f PP |
164 | "bt_component_class_get_help() returns the expected help text"); |
165 | ||
92fed4e1 | 166 | filter_comp_class = bt_plugin_borrow_filter_component_class_by_name_const( |
d94d92ac | 167 | plugin, "filter"); |
cbb9e0b1 | 168 | ok(filter_comp_class, |
92fed4e1 | 169 | "bt_plugin_borrow_filter_component_class_by_name_const() finds a filter component class"); |
9c08c816 | 170 | params = bt_value_integer_signed_create_init(23); |
25583cd0 | 171 | BT_ASSERT(params); |
3c729b9a PP |
172 | query_exec = bt_query_executor_create( |
173 | bt_component_class_filter_as_component_class_const( | |
174 | filter_comp_class), "get-something", params); | |
175 | BT_ASSERT(query_exec); | |
176 | ret = bt_query_executor_query(query_exec, &results); | |
0d72b8c3 | 177 | ok(ret == 0 && results, "bt_query_executor_query() succeeds"); |
393729a6 | 178 | BT_ASSERT(bt_value_is_array(results) && bt_value_array_get_length(results) == 2); |
05e21286 | 179 | object = bt_value_array_borrow_element_by_index_const(results, 0); |
33f4e1fd | 180 | BT_ASSERT(bt_value_is_string(object)); |
601b0d3c | 181 | object_str = bt_value_string_get(object); |
a67681c1 PP |
182 | ok(strcmp(object_str, "get-something") == 0, |
183 | "bt_component_class_query() receives the expected object name"); | |
05e21286 | 184 | res_params = bt_value_array_borrow_element_by_index_const(results, 1); |
cd933d89 | 185 | ok(bt_value_is_equal(res_params, params), |
a67681c1 | 186 | "bt_component_class_query() receives the expected parameters"); |
cbb9e0b1 | 187 | |
c5b9b441 | 188 | bt_component_class_sink_get_ref(sink_comp_class); |
c5b9b441 | 189 | BT_PLUGIN_SET_PUT_REF_AND_RESET(plugin_set); |
056deb59 | 190 | graph = bt_graph_create(0); |
25583cd0 | 191 | BT_ASSERT(graph); |
0d72b8c3 | 192 | graph_ret = bt_graph_add_sink_component(graph, sink_comp_class, |
e874da19 | 193 | "the-sink", NULL, BT_LOGGING_LEVEL_NONE, &sink_component); |
d24d5663 | 194 | ok(graph_ret == BT_GRAPH_ADD_COMPONENT_STATUS_OK && sink_component, |
0d72b8c3 | 195 | "bt_graph_add_sink_component() still works after the plugin object is destroyed"); |
c5b9b441 | 196 | bt_graph_put_ref(graph); |
cbb9e0b1 PP |
197 | |
198 | free(sfs_path); | |
c5b9b441 PP |
199 | bt_component_class_sink_put_ref(sink_comp_class); |
200 | bt_value_put_ref(results); | |
201 | bt_value_put_ref(params); | |
202 | bt_query_executor_put_ref(query_exec); | |
cbb9e0b1 PP |
203 | } |
204 | ||
205 | static void test_create_all_from_dir(const char *plugin_dir) | |
206 | { | |
b19ff26f | 207 | const bt_plugin_set *plugin_set; |
d24d5663 | 208 | bt_plugin_find_all_from_dir_status status; |
cbb9e0b1 PP |
209 | |
210 | diag("create from all test below"); | |
211 | ||
9736d991 PP |
212 | status = bt_plugin_find_all_from_dir(NON_EXISTING_PATH, BT_FALSE, |
213 | BT_FALSE, &plugin_set); | |
d24d5663 | 214 | ok(status == BT_PLUGIN_FIND_ALL_FROM_DIR_STATUS_ERROR, |
c8db3219 | 215 | "bt_plugin_find_all_from_dir() fails with an invalid path"); |
758932ce | 216 | bt_current_thread_clear_error(); |
cbb9e0b1 | 217 | |
9736d991 PP |
218 | plugin_set = NULL; |
219 | status = bt_plugin_find_all_from_dir(plugin_dir, BT_FALSE, BT_FALSE, | |
220 | &plugin_set); | |
d24d5663 | 221 | ok(status == BT_PLUGIN_FIND_ALL_FROM_DIR_STATUS_OK, |
9736d991 PP |
222 | "bt_plugin_find_all_from_dir() succeeds with a valid path"); |
223 | ok(plugin_set, | |
224 | "bt_plugin_find_all_from_dir() returns a plugin set with a valid path"); | |
cbb9e0b1 PP |
225 | |
226 | /* 2 or 4, if `.la` files are considered or not */ | |
a8ff38ef PP |
227 | ok(bt_plugin_set_get_plugin_count(plugin_set) == 2 || |
228 | bt_plugin_set_get_plugin_count(plugin_set) == 4, | |
c8db3219 | 229 | "bt_plugin_find_all_from_dir() returns the expected number of plugin objects"); |
cbb9e0b1 | 230 | |
c5b9b441 | 231 | bt_plugin_set_put_ref(plugin_set); |
cbb9e0b1 PP |
232 | } |
233 | ||
2b43acf9 | 234 | static void test_find(const char *plugin_dir) |
a8b3f23b | 235 | { |
8a6001c5 | 236 | int ret; |
b19ff26f | 237 | const bt_plugin *plugin; |
a8b3f23b | 238 | char *plugin_path; |
d24d5663 | 239 | bt_plugin_find_status status; |
a8b3f23b | 240 | |
577fa92f PP |
241 | ok(bt_plugin_find(NON_EXISTING_PATH, BT_TRUE, BT_FALSE, BT_FALSE, |
242 | BT_FALSE, BT_FALSE, &plugin) == BT_PLUGIN_FIND_STATUS_NOT_FOUND, | |
9736d991 | 243 | "bt_plugin_find() returns BT_PLUGIN_STATUS_NOT_FOUND with an unknown plugin name"); |
84095ea4 MJ |
244 | ret = asprintf(&plugin_path, "%s" G_SEARCHPATH_SEPARATOR_S |
245 | G_DIR_SEPARATOR_S "ec1d09e5-696c-442e-b1c3-f9c6cf7f5958" | |
246 | G_SEARCHPATH_SEPARATOR_S G_SEARCHPATH_SEPARATOR_S | |
247 | G_SEARCHPATH_SEPARATOR_S "%s" G_SEARCHPATH_SEPARATOR_S | |
248 | "8db46494-a398-466a-9649-c765ae077629" | |
249 | G_SEARCHPATH_SEPARATOR_S, | |
a8b3f23b | 250 | NON_EXISTING_PATH, plugin_dir); |
25583cd0 | 251 | BT_ASSERT(ret > 0 && plugin_path); |
aacfaf40 | 252 | g_setenv("BABELTRACE_PLUGIN_PATH", plugin_path, 1); |
9736d991 | 253 | plugin = NULL; |
577fa92f PP |
254 | status = bt_plugin_find("test_minimal", BT_TRUE, BT_FALSE, BT_FALSE, |
255 | BT_FALSE, BT_FALSE, &plugin); | |
d24d5663 | 256 | ok(status == BT_PLUGIN_FIND_STATUS_OK, |
2b43acf9 | 257 | "bt_plugin_find() succeeds with a plugin name it can find"); |
9736d991 | 258 | ok(plugin, "bt_plugin_find() returns a plugin object"); |
a8b3f23b | 259 | ok(strcmp(bt_plugin_get_author(plugin), "Janine Sutto") == 0, |
2b43acf9 | 260 | "bt_plugin_find() finds the correct plugin for a given name"); |
c5b9b441 | 261 | BT_PLUGIN_PUT_REF_AND_RESET(plugin); |
a8b3f23b PP |
262 | free(plugin_path); |
263 | } | |
264 | ||
cbb9e0b1 PP |
265 | int main(int argc, char **argv) |
266 | { | |
267 | int ret; | |
268 | const char *plugin_dir; | |
269 | ||
270 | if (argc != 2) { | |
271 | puts("Usage: test_plugin plugin_directory"); | |
272 | ret = 1; | |
273 | goto end; | |
274 | } | |
275 | ||
276 | plugin_dir = argv[1]; | |
277 | plan_tests(NR_TESTS); | |
cbb9e0b1 PP |
278 | test_minimal(plugin_dir); |
279 | test_sfs(plugin_dir); | |
280 | test_create_all_from_dir(plugin_dir); | |
2b43acf9 | 281 | test_find(plugin_dir); |
cbb9e0b1 PP |
282 | ret = exit_status(); |
283 | end: | |
284 | return ret; | |
285 | } |