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 <kdbmacros.h>
10 : #include <stdlib.h>
11 :
12 : #ifdef HAVE_KDBCONFIG_H
13 : #include "kdbconfig.h"
14 : #endif
15 :
16 : #include <tests_plugin.h>
17 :
18 : static void init_env (void)
19 : {
20 2 : setenv ("PYTHONDONTWRITEBYTECODE", "1", 1);
21 : }
22 :
23 : char filebuf[KDB_MAX_PATH_LENGTH + 1];
24 : static char * srcdir_rewrite = NULL;
25 10 : static char * python_file (const char * filename)
26 : {
27 10 : if (!srcdir_rewrite)
28 : {
29 : /* no rewrite. just append our plugin name */
30 5 : strcpy (filebuf, ELEKTRA_STRINGIFY (PYTHON_PLUGIN_NAME));
31 5 : strcat (strcat (filebuf, "/"), filename);
32 5 : return srcdir_file (filebuf);
33 : }
34 :
35 : /* wipe old value */
36 5 : *srcdir_rewrite = '\0';
37 :
38 : /* append plugin name and delete last character */
39 5 : strcat (strcat (filebuf, "/"), ELEKTRA_STRINGIFY (PYTHON_PLUGIN_NAME));
40 5 : *(filebuf + strlen (filebuf) - 1) = '\0';
41 :
42 5 : strcat (strcat (filebuf, "/"), filename);
43 5 : return filebuf;
44 : }
45 :
46 : // test simple variable passing
47 2 : static void test_variable_passing (void)
48 : {
49 2 : printf ("Testing simple variable passing...\n");
50 :
51 2 : KeySet * conf = ksNew (1, keyNew ("user/script", KEY_VALUE, python_file ("python_plugin.py"), KEY_END),
52 : keyNew ("user/shutdown", KEY_VALUE, "1", KEY_END), keyNew ("user/print", KEY_END),
53 : keyNew ("user/python/path", KEY_VALUE, ".", KEY_END), KS_END);
54 2 : PLUGIN_OPEN (ELEKTRA_STRINGIFY (PYTHON_PLUGIN_NAME));
55 :
56 2 : Key * parentKey = keyNew ("user/from_c", KEY_END);
57 2 : KeySet * ks = ksNew (0, KS_END);
58 2 : succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
59 2 : exit_if_fail (ksGetSize (ks) == 1, "keyset size is still 0");
60 2 : succeed_if_same_string (keyName (ksHead (ks)), "user/from_python");
61 :
62 2 : ksDel (ks);
63 2 : keyDel (parentKey);
64 :
65 2 : PLUGIN_CLOSE ();
66 2 : }
67 :
68 : // test loading python twice
69 2 : static void test_two_scripts (void)
70 : {
71 2 : printf ("Testing loading of two active python plugins...\n");
72 :
73 2 : KeySet * modules = ksNew (0, KS_END);
74 2 : elektraModulesInit (modules, 0);
75 :
76 2 : KeySet * conf = ksNew (2, keyNew ("user/script", KEY_VALUE, python_file ("python_plugin.py"), KEY_END),
77 : keyNew ("user/shutdown", KEY_VALUE, "1", KEY_END), keyNew ("user/python/path", KEY_VALUE, ".", KEY_END),
78 : keyNew ("user/print", KEY_END), KS_END);
79 :
80 2 : KeySet * conf2 = ksNew (2, keyNew ("user/script", KEY_VALUE, python_file ("python_plugin2.py"), KEY_END),
81 : keyNew ("user/shutdown", KEY_VALUE, "1", KEY_END), keyNew ("user/python/path", KEY_VALUE, ".", KEY_END),
82 : keyNew ("user/print", KEY_END), KS_END);
83 :
84 2 : Key * errorKey = keyNew ("", KEY_END);
85 2 : Plugin * plugin = elektraPluginOpen (ELEKTRA_STRINGIFY (PYTHON_PLUGIN_NAME), modules, conf, errorKey);
86 2 : succeed_if (output_warnings (errorKey), "warnings in kdbOpen");
87 2 : succeed_if (output_error (errorKey), "errors in kdbOpen");
88 2 : exit_if_fail (plugin != NULL, "unable to load python plugin");
89 2 : keyDel (errorKey);
90 :
91 2 : Key * errorKey2 = keyNew ("", KEY_END);
92 2 : Plugin * plugin2 = elektraPluginOpen (ELEKTRA_STRINGIFY (PYTHON_PLUGIN_NAME), modules, conf2, errorKey2);
93 2 : succeed_if (output_warnings (errorKey2), "warnings in kdbOpen");
94 2 : succeed_if (output_error (errorKey2), "errors in kdbOpen");
95 2 : exit_if_fail (plugin2 != NULL, "unable to load python plugin again");
96 2 : keyDel (errorKey2);
97 :
98 2 : elektraPluginClose (plugin2, 0);
99 2 : elektraPluginClose (plugin, 0);
100 2 : elektraModulesClose (modules, 0);
101 2 : ksDel (modules);
102 2 : }
103 :
104 : // simple return value test
105 2 : static void test_fail (void)
106 : {
107 2 : printf ("Testing return values from python functions...\n");
108 :
109 2 : KeySet * conf = ksNew (2, keyNew ("user/script", KEY_VALUE, python_file ("python_plugin_fail.py"), KEY_END),
110 : keyNew ("user/shutdown", KEY_VALUE, "1", KEY_END), keyNew ("user/python/path", KEY_VALUE, ".", KEY_END),
111 : keyNew ("user/print", KEY_END), KS_END);
112 2 : PLUGIN_OPEN (ELEKTRA_STRINGIFY (PYTHON_PLUGIN_NAME));
113 :
114 2 : Key * parentKey = keyNew ("user/tests/from_c", KEY_END);
115 2 : KeySet * ks = ksNew (0, KS_END);
116 :
117 2 : succeed_if (plugin->kdbGet (plugin, ks, parentKey) == -1, "call to kdbGet didn't fail");
118 2 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) == -1, "call to kdbSet didn't fail");
119 2 : succeed_if (plugin->kdbError (plugin, ks, parentKey) == -1, "call to kdbError didn't fail");
120 :
121 2 : ksDel (ks);
122 2 : keyDel (parentKey);
123 :
124 2 : PLUGIN_CLOSE ();
125 2 : }
126 :
127 : // test script with wrong class name
128 2 : static void test_wrong (void)
129 : {
130 2 : printf ("Testing python script with wrong class name...\n");
131 :
132 2 : KeySet * modules = ksNew (0, KS_END);
133 2 : elektraModulesInit (modules, 0);
134 :
135 2 : KeySet * conf = ksNew (2, keyNew ("user/script", KEY_VALUE, python_file ("python_plugin_wrong.py"), KEY_END),
136 : keyNew ("user/shutdown", KEY_VALUE, "1", KEY_END), keyNew ("user/python/path", KEY_VALUE, ".", KEY_END),
137 : keyNew ("user/print", KEY_END), KS_END);
138 :
139 2 : Key * errorKey = keyNew ("", KEY_END);
140 2 : Plugin * plugin = elektraPluginOpen (ELEKTRA_STRINGIFY (PYTHON_PLUGIN_NAME), modules, conf, errorKey);
141 2 : succeed_if (!output_warnings (errorKey), "we expect some warnings");
142 2 : succeed_if (!output_error (errorKey), "we expect some errors");
143 2 : succeed_if (plugin == NULL, "python plugin shouldn't be loadable");
144 2 : keyDel (errorKey);
145 :
146 2 : elektraModulesClose (modules, 0);
147 2 : ksDel (modules);
148 2 : }
149 :
150 2 : int main (int argc, char ** argv)
151 : {
152 2 : printf ("PYTHON TESTS\n");
153 2 : printf ("==================\n\n");
154 :
155 2 : init (argc, argv);
156 2 : if (argc > 1)
157 : {
158 2 : strncpy (filebuf, argv[1], sizeof (filebuf) - 1);
159 : /* our files are in pythons plugin directory
160 : * -> rewrite srcdir from xxx/python2 to xxx/python
161 : */
162 2 : if (strlen (filebuf) > strlen ("python2") && !strcmp (filebuf + strlen (filebuf) - strlen ("python2"), "python2"))
163 : {
164 1 : srcdir_rewrite = filebuf + strlen (filebuf) - 1;
165 1 : *srcdir_rewrite = '\0';
166 : }
167 : }
168 : init_env ();
169 :
170 2 : test_variable_passing ();
171 2 : test_two_scripts ();
172 :
173 2 : printf ("\n");
174 2 : printf ("========================================================================\n");
175 2 : printf ("NOTE: The following errors are intended. We're testing error conditions!\n");
176 2 : printf ("========================================================================\n");
177 2 : test_fail ();
178 2 : test_wrong ();
179 :
180 2 : print_result ("test_python");
181 :
182 2 : return nbError;
183 : }
|