Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : */
8 :
9 : #include <tests_internal.h>
10 :
11 : #include <kdbinternal.h>
12 :
13 : #include <langinfo.h>
14 :
15 : #include "resolver.h"
16 :
17 20 : KeySet * set_pluginconf (void)
18 : {
19 20 : return ksNew (10, keyNew ("system/path", KEY_VALUE, KDB_DB_FILE, KEY_END), keyNew ("user/path", KEY_VALUE, "elektra.ecf", KEY_END),
20 : KS_END);
21 : }
22 :
23 :
24 2 : void test_resolve (void)
25 : {
26 2 : int pathLen = tempHomeLen + 1 + strlen (KDB_DB_USER) + 12 + 1;
27 2 : char * path = elektraMalloc (pathLen);
28 2 : exit_if_fail (path != 0, "elektraMalloc failed");
29 2 : snprintf (path, pathLen, "%s/%s/elektra.ecf", tempHome, KDB_DB_USER);
30 :
31 2 : printf ("Resolve Filename\n");
32 :
33 2 : KeySet * modules = ksNew (0, KS_END);
34 2 : elektraModulesInit (modules, 0);
35 :
36 2 : Key * parentKey = keyNew ("system", KEY_END);
37 2 : Plugin * plugin = elektraPluginOpen ("resolver", modules, set_pluginconf (), 0);
38 2 : exit_if_fail (plugin, "could not load resolver plugin");
39 :
40 2 : KeySet * test_config = set_pluginconf ();
41 2 : KeySet * config = elektraPluginGetConfig (plugin);
42 2 : succeed_if (config != 0, "there should be a config");
43 2 : compare_keyset (config, test_config);
44 2 : ksDel (test_config);
45 :
46 2 : succeed_if (plugin->kdbOpen != 0, "no open pointer");
47 2 : succeed_if (plugin->kdbClose != 0, "no open pointer");
48 2 : succeed_if (plugin->kdbGet != 0, "no open pointer");
49 2 : succeed_if (plugin->kdbSet != 0, "no open pointer");
50 2 : succeed_if (plugin->kdbError != 0, "no open pointer");
51 2 : succeed_if (plugin->kdbCommit != 0, "no open pointer");
52 :
53 2 : succeed_if (!strncmp (plugin->name, "resolver", strlen ("resolver")), "got wrong name");
54 :
55 2 : resolverHandles * h = elektraPluginGetData (plugin);
56 2 : exit_if_fail (h != 0, "no plugin handle");
57 2 : succeed_if_same_string (h->system.path, "elektra.ecf");
58 : if (KDB_DB_SYSTEM[0] == '~')
59 : {
60 : // only check filename and issue warning
61 : const char * lastSlash = strrchr (h->system.filename, '/');
62 : warn_if_fail (0, "using home based KDB_DB_SYSTEM falls back to less strict testing");
63 : succeed_if (lastSlash != NULL && strcmp (lastSlash, "/elektra.ecf") == 0, "wrong filename with home based KDB_DB_SYSTEM");
64 : }
65 : else
66 : {
67 2 : succeed_if_same_string (h->system.filename, KDB_DB_SYSTEM "/elektra.ecf");
68 : }
69 2 : succeed_if_same_string (h->user.path, "elektra.ecf");
70 2 : succeed_if_same_string (h->user.filename, path);
71 2 : plugin->kdbClose (plugin, parentKey);
72 :
73 : // reinit with system path only
74 2 : plugin->kdbOpen (plugin, parentKey);
75 2 : h = elektraPluginGetData (plugin);
76 2 : exit_if_fail (h != 0, "no plugin handle");
77 2 : succeed_if_same_string (h->system.path, "elektra.ecf");
78 : if (KDB_DB_SYSTEM[0] == '~')
79 : {
80 : // only check filename and issue warning
81 : const char * lastSlash = strrchr (h->system.filename, '/');
82 : warn_if_fail (0, "using home based KDB_DB_SYSTEM falls back to less strict testing");
83 : succeed_if (lastSlash != NULL && strcmp (lastSlash, "/elektra.ecf") == 0, "wrong filename with home based KDB_DB_SYSTEM");
84 : }
85 : else
86 : {
87 2 : succeed_if_same_string (h->system.filename, KDB_DB_SYSTEM "/elektra.ecf");
88 : }
89 2 : succeed_if (h->user.filename == NULL, "user was initialized, but is not needed");
90 2 : plugin->kdbClose (plugin, parentKey);
91 :
92 2 : keyDel (parentKey);
93 2 : elektraPluginClose (plugin, 0);
94 2 : elektraModulesClose (modules, 0);
95 2 : ksDel (modules);
96 2 : elektraFree (path);
97 2 : }
98 :
99 2 : void test_name (void)
100 : {
101 2 : printf ("Resolve Name\n");
102 :
103 2 : KeySet * modules = ksNew (0, KS_END);
104 2 : elektraModulesInit (modules, 0);
105 :
106 2 : Plugin * plugin = elektraPluginOpen ("resolver", modules, set_pluginconf (), 0);
107 2 : exit_if_fail (plugin, "could not load resolver plugin");
108 :
109 2 : KeySet * test_config = set_pluginconf ();
110 2 : KeySet * config = elektraPluginGetConfig (plugin);
111 2 : succeed_if (config != 0, "there should be a config");
112 2 : compare_keyset (config, test_config);
113 2 : ksDel (test_config);
114 :
115 2 : succeed_if (plugin->kdbOpen != 0, "no open pointer");
116 2 : succeed_if (plugin->kdbClose != 0, "no open pointer");
117 2 : succeed_if (plugin->kdbGet != 0, "no open pointer");
118 2 : succeed_if (plugin->kdbSet != 0, "no open pointer");
119 2 : succeed_if (plugin->kdbError != 0, "no open pointer");
120 2 : succeed_if (plugin->kdbCommit != 0, "no open pointer");
121 :
122 2 : succeed_if (!strncmp (plugin->name, "resolver", strlen ("resolver")), "got wrong name");
123 :
124 2 : resolverHandles * h = elektraPluginGetData (plugin);
125 2 : succeed_if (h != 0, "no plugin handle");
126 :
127 2 : Key * parentKey = keyNew ("system", KEY_END);
128 2 : plugin->kdbGet (plugin, 0, parentKey);
129 : if (KDB_DB_SYSTEM[0] == '~')
130 : {
131 : // only check filename and issue warning
132 : const char * lastSlash = strrchr (keyString (parentKey), '/');
133 : warn_if_fail (0, "using home based KDB_DB_SYSTEM falls back to less strict testing");
134 : succeed_if (lastSlash != NULL && strcmp (lastSlash, "/elektra.ecf") == 0, "wrong filename with home based KDB_DB_SYSTEM");
135 : }
136 : else
137 : {
138 2 : succeed_if_same_string (keyString (parentKey), KDB_DB_SYSTEM "/elektra.ecf");
139 : }
140 :
141 2 : keyDel (parentKey);
142 2 : elektraPluginClose (plugin, 0);
143 2 : elektraModulesClose (modules, 0);
144 2 : ksDel (modules);
145 2 : }
146 :
147 2 : void test_lockname (void)
148 : {
149 2 : printf ("Resolve Dirname\n");
150 :
151 2 : KeySet * modules = ksNew (0, KS_END);
152 2 : elektraModulesInit (modules, 0);
153 :
154 2 : Plugin * plugin = elektraPluginOpen ("resolver", modules, set_pluginconf (), 0);
155 2 : exit_if_fail (plugin, "could not load resolver plugin");
156 :
157 2 : KeySet * test_config = set_pluginconf ();
158 2 : KeySet * config = elektraPluginGetConfig (plugin);
159 2 : succeed_if (config != 0, "there should be a config");
160 2 : compare_keyset (config, test_config);
161 2 : ksDel (test_config);
162 :
163 2 : succeed_if (plugin->kdbOpen != 0, "no open pointer");
164 2 : succeed_if (plugin->kdbClose != 0, "no open pointer");
165 2 : succeed_if (plugin->kdbGet != 0, "no open pointer");
166 2 : succeed_if (plugin->kdbSet != 0, "no open pointer");
167 2 : succeed_if (plugin->kdbError != 0, "no open pointer");
168 2 : succeed_if (plugin->kdbCommit != 0, "no open pointer");
169 :
170 2 : succeed_if (!strncmp (plugin->name, "resolver", strlen ("resolver")), "got wrong name");
171 :
172 2 : resolverHandles * h = elektraPluginGetData (plugin);
173 2 : succeed_if (h != 0, "no plugin handle");
174 :
175 2 : Key * parentKey = keyNew ("system", KEY_END);
176 2 : plugin->kdbGet (plugin, 0, parentKey);
177 : if (h && KDB_DB_SYSTEM[0] == '~')
178 : {
179 : // issue warning and then only check if dirname ends with the user-independent part of KDB_DB_SYSTEM
180 : const char * firstSlash = strchr (KDB_DB_SYSTEM, '/');
181 : warn_if_fail (0, "using home based KDB_DB_SYSTEM falls back to less strict testing");
182 : succeed_if (firstSlash == NULL ||
183 : strcmp (h->system.dirname + strlen (h->system.dirname) - strlen (firstSlash), firstSlash) == 0,
184 : "resulting filename not correct with home based KDB_DB_SYSTEM");
185 : }
186 : else
187 : {
188 2 : succeed_if (h && !strcmp (h->system.dirname, KDB_DB_SYSTEM), "resulting filename not correct");
189 : }
190 :
191 2 : keyDel (parentKey);
192 2 : elektraPluginClose (plugin, 0);
193 2 : elektraModulesClose (modules, 0);
194 2 : ksDel (modules);
195 2 : }
196 :
197 2 : void test_tempname (void)
198 : {
199 2 : printf ("Resolve Tempname\n");
200 :
201 2 : KeySet * modules = ksNew (0, KS_END);
202 2 : elektraModulesInit (modules, 0);
203 :
204 2 : Plugin * plugin = elektraPluginOpen ("resolver", modules, set_pluginconf (), 0);
205 2 : exit_if_fail (plugin, "could not load resolver plugin");
206 :
207 2 : KeySet * test_config = set_pluginconf ();
208 2 : KeySet * config = elektraPluginGetConfig (plugin);
209 2 : succeed_if (config != 0, "there should be a config");
210 2 : compare_keyset (config, test_config);
211 2 : ksDel (test_config);
212 :
213 2 : succeed_if (plugin->kdbOpen != 0, "no open pointer");
214 2 : succeed_if (plugin->kdbClose != 0, "no open pointer");
215 2 : succeed_if (plugin->kdbGet != 0, "no open pointer");
216 2 : succeed_if (plugin->kdbSet != 0, "no open pointer");
217 2 : succeed_if (plugin->kdbError != 0, "no open pointer");
218 2 : succeed_if (plugin->kdbCommit != 0, "no open pointer");
219 :
220 2 : succeed_if (!strncmp (plugin->name, "resolver", strlen ("resolver")), "got wrong name");
221 :
222 2 : resolverHandles * h = elektraPluginGetData (plugin);
223 2 : succeed_if (h != 0, "no plugin handle");
224 :
225 2 : Key * parentKey = keyNew ("system", KEY_END);
226 2 : plugin->kdbGet (plugin, 0, parentKey);
227 : if (h && KDB_DB_SYSTEM[0] == '~')
228 : {
229 : // only check filename and issue warning
230 : const char * lastSlash = strrchr (h->system.tempfile, '/');
231 : warn_if_fail (0, "using home based KDB_DB_SYSTEM falls back to less strict testing");
232 : succeed_if (lastSlash != NULL && !strncmp (lastSlash, "/elektra.ecf", sizeof ("/elektra.ecf") - 1),
233 : "resulting filename not correct with home based KDB_DB_SYSTEM");
234 : }
235 : else
236 : {
237 2 : succeed_if (h && !strncmp (h->system.tempfile, KDB_DB_SYSTEM "/elektra.ecf", sizeof (KDB_DB_SYSTEM "/elektra.ecf") - 1),
238 : "resulting filename not correct");
239 : }
240 :
241 :
242 2 : keyDel (parentKey);
243 2 : elektraPluginClose (plugin, 0);
244 2 : elektraModulesClose (modules, 0);
245 2 : ksDel (modules);
246 2 : }
247 :
248 2 : void test_checkfile (void)
249 : {
250 2 : printf ("Check file\n");
251 :
252 2 : KeySet * modules = ksNew (0, KS_END);
253 2 : elektraModulesInit (modules, 0);
254 2 : Plugin * plugin = elektraPluginOpen ("resolver", modules, set_pluginconf (), 0);
255 2 : exit_if_fail (plugin, "did not find a resolver");
256 :
257 2 : Key * root = keyNew ("system/elektra/modules", KEY_END);
258 2 : keyAddBaseName (root, plugin->name);
259 :
260 2 : KeySet * contract = ksNew (5, KS_END);
261 :
262 2 : plugin->kdbGet (plugin, contract, root);
263 2 : keyAddName (root, "/exports/checkfile");
264 2 : Key * found = ksLookup (contract, root, 0);
265 2 : exit_if_fail (found, "did not find checkfile symbol");
266 :
267 : typedef int (*func_t) (const char *);
268 : union
269 : {
270 : func_t f;
271 : void * v;
272 : } conversation;
273 :
274 2 : succeed_if (keyGetBinary (found, &conversation.v, sizeof (conversation)) == sizeof (conversation), "could not get binary");
275 2 : func_t checkFile = conversation.f;
276 :
277 :
278 2 : succeed_if (checkFile ("valid") == 1, "valid file not recognised");
279 2 : succeed_if (checkFile ("/valid") == 0, "valid absolute file not recognised");
280 2 : succeed_if (checkFile ("/absolute/valid") == 0, "valid absolute file not recognised");
281 2 : succeed_if (checkFile ("../valid") == -1, "invalid file not recognised");
282 2 : succeed_if (checkFile ("valid/..") == -1, "invalid file not recognised");
283 2 : succeed_if (checkFile ("/../valid") == -1, "invalid absolute file not recognised");
284 2 : succeed_if (checkFile ("/valid/..") == -1, "invalid absolute file not recognised");
285 2 : succeed_if (checkFile ("very..strict") == -1, "resolver is currently very strict");
286 2 : succeed_if (checkFile ("very/..strict") == -1, "resolver is currently very strict");
287 2 : succeed_if (checkFile ("very../strict") == -1, "resolver is currently very strict");
288 2 : succeed_if (checkFile ("very/../strict") == -1, "resolver is currently very strict");
289 2 : succeed_if (checkFile ("/") == -1, "invalid absolute file not recognised");
290 2 : succeed_if (checkFile (".") == -1, "invalid file not recognised");
291 2 : succeed_if (checkFile ("..") == -1, "invalid file not recognised");
292 :
293 2 : ksDel (contract);
294 2 : keyDel (root);
295 :
296 2 : elektraPluginClose (plugin, 0);
297 2 : elektraModulesClose (modules, 0);
298 2 : ksDel (modules);
299 2 : }
300 :
301 2 : static void check_xdg (void)
302 : {
303 2 : KeySet * modules = ksNew (0, KS_END);
304 2 : elektraModulesInit (modules, 0);
305 2 : Plugin * plugin = elektraPluginOpen ("resolver", modules, set_pluginconf (), 0);
306 2 : exit_if_fail (plugin, "did not find a resolver");
307 :
308 2 : int abort = 0;
309 2 : if (strchr (plugin->name, 'x') != NULL)
310 : {
311 0 : printf ("Will abort successfully because default resolver is an XDG resolver (%s)\n", plugin->name);
312 0 : abort = 1;
313 : }
314 :
315 2 : elektraPluginClose (plugin, 0);
316 2 : elektraModulesClose (modules, 0);
317 2 : ksDel (modules);
318 :
319 2 : if (abort)
320 : {
321 0 : exit (0);
322 : }
323 2 : }
324 :
325 :
326 2 : int main (int argc, char ** argv)
327 : {
328 2 : printf (" RESOLVER TESTS\n");
329 2 : printf ("====================\n\n");
330 :
331 2 : init (argc, argv);
332 :
333 2 : test_checkfile ();
334 :
335 2 : check_xdg ();
336 :
337 2 : test_resolve ();
338 2 : test_name ();
339 2 : test_lockname ();
340 2 : test_tempname ();
341 :
342 :
343 2 : print_result ("testmod_resolver");
344 :
345 2 : return nbError;
346 : }
|