forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_backend.c
265 lines (211 loc) · 11.4 KB
/
test_backend.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/**
* @file
*
* @brief Test cases for how to build a backend out of system:/elektra/mountpoints/<name>
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*/
#include <../../src/libs/elektra/backend.c>
#include <tests_internal.h>
KeySet * set_simple (void)
{
return ksNew (50, keyNew ("system:/elektra/mountpoints/simple", KEY_END),
keyNew ("system:/elektra/mountpoints/simple/config", KEY_END),
keyNew ("system:/elektra/mountpoints/simple/config/anything", KEY_VALUE, "backend", KEY_END),
keyNew ("system:/elektra/mountpoints/simple/config/more", KEY_END),
keyNew ("system:/elektra/mountpoints/simple/config/more/config", KEY_END),
keyNew ("system:/elektra/mountpoints/simple/config/more/config/below", KEY_END),
keyNew ("system:/elektra/mountpoints/simple/config/path", KEY_END),
keyNew ("system:/elektra/mountpoints/simple/errorplugins", KEY_END),
keyNew ("system:/elektra/mountpoints/simple/errorplugins/#1" KDB_DEFAULT_STORAGE, KEY_END),
keyNew ("system:/elektra/mountpoints/simple/getplugins", KEY_END),
keyNew ("system:/elektra/mountpoints/simple/getplugins/#1" KDB_DEFAULT_STORAGE, KEY_END),
keyNew ("system:/elektra/mountpoints/simple/getplugins/#1" KDB_DEFAULT_STORAGE "/config", KEY_END),
keyNew ("system:/elektra/mountpoints/simple/getplugins/#1" KDB_DEFAULT_STORAGE "/config/anything", KEY_VALUE,
"plugin", KEY_END),
keyNew ("system:/elektra/mountpoints/simple/getplugins/#1" KDB_DEFAULT_STORAGE "/config/more", KEY_END),
keyNew ("system:/elektra/mountpoints/simple/getplugins/#1" KDB_DEFAULT_STORAGE "/config/more/config", KEY_END),
keyNew ("system:/elektra/mountpoints/simple/getplugins/#1" KDB_DEFAULT_STORAGE "/config/more/config/below", KEY_END),
keyNew ("system:/elektra/mountpoints/simple/getplugins/#1" KDB_DEFAULT_STORAGE "/config/path", KEY_END),
keyNew ("system:/elektra/mountpoints/simple/mountpoint", KEY_VALUE, "user:/tests/backend/simple", KEY_END),
keyNew ("system:/elektra/mountpoints/simple/setplugins", KEY_END),
keyNew ("system:/elektra/mountpoints/simple/setplugins/#1" KDB_DEFAULT_STORAGE, KEY_END),
keyNew ("system:/elektra/mountpoints/simple/errorplugins", KEY_END),
keyNew ("system:/elektra/mountpoints/simple/errorplugins/#1" KDB_DEFAULT_STORAGE, KEY_END), KS_END);
}
KeySet * set_pluginconf (void)
{
return ksNew (10, keyNew ("system:/anything", KEY_VALUE, "backend", KEY_END), keyNew ("system:/more", KEY_END),
keyNew ("system:/more/config", KEY_END), keyNew ("system:/more/config/below", KEY_END),
keyNew ("system:/path", KEY_END), keyNew ("user:/anything", KEY_VALUE, "plugin", KEY_END),
keyNew ("user:/more", KEY_END), keyNew ("user:/more/config", KEY_END), keyNew ("user:/more/config/below", KEY_END),
keyNew ("user:/path", KEY_END), KS_END);
}
static void test_simple (void)
{
printf ("Test simple building of backend\n");
KeySet * modules = ksNew (0, KS_END);
elektraModulesInit (modules, 0);
KeySet * global = ksNew (0, KS_END);
Key * errorKey = 0;
Backend * backend = backendOpen (set_simple (), modules, global, errorKey);
succeed_if (backend->errorplugins[0] == 0, "there should be no plugin");
succeed_if (backend->errorplugins[2] == 0, "there should be no plugin");
succeed_if (backend->errorplugins[3] == 0, "there should be no plugin");
succeed_if (backend->errorplugins[4] == 0, "there should be no plugin");
succeed_if (backend->errorplugins[5] == 0, "there should be no plugin");
succeed_if (backend->errorplugins[6] == 0, "there should be no plugin");
succeed_if (backend->errorplugins[7] == 0, "there should be no plugin");
succeed_if (backend->errorplugins[8] == 0, "there should be no plugin");
succeed_if (backend->errorplugins[9] == 0, "there should be no plugin");
exit_if_fail (backend->errorplugins[1] != 0, "there should be a plugin");
succeed_if (backend->getplugins[0] == 0, "there should be no plugin");
succeed_if (backend->getplugins[2] == 0, "there should be no plugin");
succeed_if (backend->getplugins[3] == 0, "there should be no plugin");
succeed_if (backend->getplugins[4] == 0, "there should be no plugin");
succeed_if (backend->getplugins[5] == 0, "there should be no plugin");
succeed_if (backend->getplugins[6] == 0, "there should be no plugin");
succeed_if (backend->getplugins[7] == 0, "there should be no plugin");
succeed_if (backend->getplugins[8] == 0, "there should be no plugin");
succeed_if (backend->getplugins[9] == 0, "there should be no plugin");
exit_if_fail (backend->getplugins[1] != 0, "there should be a plugin");
succeed_if (backend->setplugins[0] == 0, "there should be no plugin");
succeed_if (backend->setplugins[2] == 0, "there should be no plugin");
succeed_if (backend->setplugins[3] == 0, "there should be no plugin");
succeed_if (backend->setplugins[4] == 0, "there should be no plugin");
succeed_if (backend->setplugins[5] == 0, "there should be no plugin");
succeed_if (backend->setplugins[6] == 0, "there should be no plugin");
succeed_if (backend->setplugins[7] == 0, "there should be no plugin");
succeed_if (backend->setplugins[8] == 0, "there should be no plugin");
succeed_if (backend->setplugins[9] == 0, "there should be no plugin");
exit_if_fail (backend->setplugins[1] != 0, "there should be a plugin");
Key * mp;
succeed_if ((mp = backend->mountpoint) != 0, "no mountpoint found");
succeed_if_same_string (keyName (mp), "user:/tests/backend/simple");
succeed_if_same_string (keyString (mp), "simple");
Plugin * plugin = backend->getplugins[1];
KeySet * test_config = set_pluginconf ();
KeySet * config = elektraPluginGetConfig (plugin);
succeed_if (config != 0, "there should be a config");
compare_keyset (config, test_config);
ksDel (test_config);
succeed_if (plugin->kdbGet != 0, "no get pointer");
succeed_if (plugin->kdbSet != 0, "no set pointer");
backendClose (backend, errorKey);
elektraModulesClose (modules, 0);
ksDel (modules);
ksDel (global);
}
static void test_default (void)
{
printf ("Test default " KDB_DEFAULT_STORAGE "\n");
KeySet * modules = ksNew (0, KS_END);
elektraModulesInit (modules, 0);
Plugin * plugin = elektraPluginOpen (KDB_DEFAULT_STORAGE, modules, set_pluginconf (), 0);
exit_if_fail (plugin, "KDB_DEFAULT_STORAGE: " KDB_DEFAULT_STORAGE " plugin could not be loaded");
KeySet * test_config = set_pluginconf ();
KeySet * config = elektraPluginGetConfig (plugin);
succeed_if (config != 0, "there should be a config");
compare_keyset (config, test_config);
ksDel (test_config);
succeed_if (plugin->kdbGet != 0, "no get pointer");
succeed_if (plugin->kdbSet != 0, "no set pointer");
elektraPluginClose (plugin, 0);
KeySet * global = ksNew (0, KS_END);
Backend * backend = backendOpenDefault (modules, global, KDB_DB_FILE, 0);
Key * mp;
succeed_if ((mp = backend->mountpoint) == NULL, "default backend mountpoint should be NULL");
backendClose (backend, 0);
elektraModulesClose (modules, 0);
ksDel (modules);
ksDel (global);
}
KeySet * set_backref (void)
{
return ksNew (
50, keyNew ("system:/elektra/mountpoints/backref", KEY_END),
keyNew ("system:/elektra/mountpoints/backref/config", KEY_END),
keyNew ("system:/elektra/mountpoints/backref/config/anything", KEY_VALUE, "backend", KEY_END),
keyNew ("system:/elektra/mountpoints/backref/config/more", KEY_END),
keyNew ("system:/elektra/mountpoints/backref/config/more/config", KEY_END),
keyNew ("system:/elektra/mountpoints/backref/config/more/config/below", KEY_END),
keyNew ("system:/elektra/mountpoints/backref/config/path", KEY_END),
keyNew ("system:/elektra/mountpoints/backref/errorplugins", KEY_END),
keyNew ("system:/elektra/mountpoints/backref/errorplugins/#1#" KDB_DEFAULT_STORAGE "#" KDB_DEFAULT_STORAGE "#", KEY_VALUE,
"introduce reference", KEY_END),
keyNew ("system:/elektra/mountpoints/backref/errorplugins/#1#" KDB_DEFAULT_STORAGE "#" KDB_DEFAULT_STORAGE "#/config",
KEY_END),
keyNew ("system:/elektra/mountpoints/backref/errorplugins/#1#" KDB_DEFAULT_STORAGE "#" KDB_DEFAULT_STORAGE
"#/config/anything",
KEY_VALUE, "plugin", KEY_END),
keyNew ("system:/elektra/mountpoints/backref/errorplugins/#1#" KDB_DEFAULT_STORAGE "#" KDB_DEFAULT_STORAGE "#/config/more",
KEY_END),
keyNew ("system:/elektra/mountpoints/backref/errorplugins/#1#" KDB_DEFAULT_STORAGE "#" KDB_DEFAULT_STORAGE
"#/config/more/config",
KEY_END),
keyNew ("system:/elektra/mountpoints/backref/errorplugins/#1#" KDB_DEFAULT_STORAGE "#" KDB_DEFAULT_STORAGE
"#/config/more/config/below",
KEY_END),
keyNew ("system:/elektra/mountpoints/backref/errorplugins/#1#" KDB_DEFAULT_STORAGE "#" KDB_DEFAULT_STORAGE "#/config/path",
KEY_END),
keyNew ("system:/elektra/mountpoints/backref/getplugins", KEY_END),
keyNew ("system:/elektra/mountpoints/backref/getplugins/#1#" KDB_DEFAULT_STORAGE, KEY_VALUE, "backend", KEY_END),
keyNew ("system:/elektra/mountpoints/backref/mountpoint", KEY_VALUE, "user:/tests/backend/backref", KEY_END),
keyNew ("system:/elektra/mountpoints/backref/setplugins", KEY_END),
keyNew ("system:/elektra/mountpoints/backref/setplugins/#1#" KDB_DEFAULT_STORAGE, KEY_VALUE, "reference to other default",
KEY_END),
KS_END);
}
static void test_backref (void)
{
printf ("Test back references\n");
KeySet * modules = ksNew (0, KS_END);
elektraModulesInit (modules, 0);
KeySet * global = ksNew (0, KS_END);
Backend * backend = backendOpen (set_backref (), modules, global, 0);
succeed_if (backend != 0, "there should be a backend");
succeed_if (backend->getplugins[0] == 0, "there should be no plugin");
exit_if_fail (backend->getplugins[1] != 0, "there should be a plugin");
succeed_if (backend->getplugins[2] == 0, "there should be no plugin");
succeed_if (backend->setplugins[0] == 0, "there should be no plugin");
exit_if_fail (backend->setplugins[1] != 0, "there should be a plugin");
succeed_if (backend->setplugins[2] == 0, "there should be no plugin");
Key * mp;
succeed_if ((mp = backend->mountpoint) != 0, "no mountpoint found");
succeed_if_same_string (keyName (mp), "user:/tests/backend/backref");
succeed_if_same_string (keyString (mp), "backref");
Plugin * plugin1 = backend->getplugins[1];
Plugin * plugin2 = backend->setplugins[1];
Plugin * plugin3 = backend->errorplugins[1];
succeed_if (plugin1 != 0, "there should be a plugin");
succeed_if (plugin2 != 0, "there should be a plugin");
succeed_if (plugin3 != 0, "there should be a plugin");
succeed_if (plugin1 == plugin2, "it should be the same plugin");
succeed_if (plugin2 == plugin3, "it should be the same plugin");
succeed_if (plugin1 == plugin3, "it should be the same plugin");
succeed_if (plugin1->refcounter == 3, "ref counter should be 3");
KeySet * test_config = set_pluginconf ();
KeySet * config = elektraPluginGetConfig (plugin1);
succeed_if (config != 0, "there should be a config");
compare_keyset (config, test_config);
ksDel (test_config);
succeed_if (plugin1->kdbGet != 0, "no get pointer");
succeed_if (plugin1->kdbSet != 0, "no set pointer");
succeed_if (plugin2->kdbGet != 0, "no get pointer");
succeed_if (plugin2->kdbSet != 0, "no set pointer");
backendClose (backend, 0);
elektraModulesClose (modules, 0);
ksDel (modules);
ksDel (global);
}
int main (int argc, char ** argv)
{
printf (" BACKEND TESTS\n");
printf ("====================\n\n");
init (argc, argv);
test_simple ();
test_default ();
test_backref ();
printf ("\ntest_backend RESULTS: %d test(s) done. %d error(s).\n", nbTest, nbError);
return nbError;
}