Line data Source code
1 : /**
2 : * @file
3 : *
4 : * @brief Tests for the hosts plugin
5 : *
6 : * @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
7 : *
8 : */
9 :
10 : #ifdef HAVE_KDBCONFIG_H
11 : #include "kdbconfig.h"
12 : #endif
13 :
14 : #include <stdio.h>
15 : #ifdef HAVE_STDLIB_H
16 : #include <stdlib.h>
17 : #endif
18 : #ifdef HAVE_STRING_H
19 : #include <string.h>
20 : #endif
21 :
22 : #include <tests_plugin.h>
23 :
24 2 : void test_readHostsSimple (char * fileName)
25 : {
26 2 : Key * parentKey = keyNew ("user/tests/hosts", KEY_VALUE, srcdir_file (fileName), KEY_END);
27 2 : KeySet * conf = 0;
28 2 : PLUGIN_OPEN ("hosts");
29 :
30 2 : KeySet * ks = ksNew (0, KS_END);
31 :
32 2 : succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
33 :
34 2 : Key * key = ksLookupByName (ks, "user/tests/hosts/ipv4/localhost", 0);
35 2 : exit_if_fail (key, "hostname localhost not found");
36 2 : succeed_if (strcmp ("127.0.0.1", keyValue (key)) == 0, "address not correct");
37 :
38 2 : key = ksLookupByName (ks, "user/tests/hosts/ipv4/gateway.markus-raab.org", 0);
39 2 : exit_if_fail (key, "hostname gateway.markus-raab.org not found");
40 2 : succeed_if (strcmp ("192.168.0.1", keyValue (key)) == 0, "address not correct");
41 :
42 2 : key = ksLookupByName (ks, "user/tests/hosts/ipv4/kirabyte.markus-raab.org/kira", 0);
43 2 : exit_if_fail (key, "hostname alias kira not found");
44 2 : succeed_if (strcmp ("192.168.0.5", keyValue (key)) == 0, "address not correct");
45 :
46 2 : key = ksLookupByName (ks, "user/tests/hosts/ipv6/wikipedia-sample", 0);
47 2 : exit_if_fail (key, "hostname wikipedia-sample not found");
48 2 : succeed_if (strcmp ("fd9e:21a7:a92c:2323::1", keyValue (key)) == 0, "address not correct");
49 :
50 2 : key = ksLookupByName (ks, "user/tests/hosts/ipv6/wikipedia-sample/wikipedia-alias", 0);
51 2 : exit_if_fail (key, "hostname alias wikipedia-alias not found");
52 2 : succeed_if (strcmp ("fd9e:21a7:a92c:2323::1", keyValue (key)) == 0, "address not correct");
53 :
54 :
55 2 : ksDel (ks);
56 2 : keyDel (parentKey);
57 :
58 2 : PLUGIN_CLOSE ();
59 2 : }
60 :
61 2 : void test_readInvalidIpAddress (char * fileName)
62 : {
63 2 : Key * parentKey = keyNew ("user/tests/hosts", KEY_VALUE, srcdir_file (fileName), KEY_END);
64 2 : KeySet * conf = 0;
65 2 : PLUGIN_OPEN ("hosts");
66 :
67 2 : KeySet * ks = ksNew (0, KS_END);
68 2 : succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
69 :
70 2 : Key * key = ksLookupByName (ks, "user/tests/hosts/ipv4/localhost", KDB_O_NONE);
71 2 : exit_if_fail (key, "hostname localhost not found");
72 2 : succeed_if (strcmp ("noipaddress", keyValue (key)) == 0, "address not correct");
73 :
74 2 : ksDel (ks);
75 2 : keyDel (parentKey);
76 :
77 2 : PLUGIN_CLOSE ();
78 2 : }
79 :
80 2 : void test_mixedAddresses (char * fileName)
81 : {
82 2 : Key * parentKey = keyNew ("user/tests/hosts", KEY_VALUE, srcdir_file (fileName), KEY_END);
83 2 : KeySet * conf = 0;
84 2 : PLUGIN_OPEN ("hosts");
85 :
86 2 : KeySet * ks = ksNew (0, KS_END);
87 :
88 2 : succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
89 :
90 2 : Key * key = ksLookupByName (ks, "user/tests/hosts/ipv4/ipv4host", KDB_O_NONE);
91 2 : exit_if_fail (key, "hostname ipv4host not found");
92 2 : succeed_if (strcmp ("127.0.0.1", keyValue (key)) == 0, "address not correct");
93 :
94 2 : key = ksLookupByName (ks, "user/tests/hosts/ipv4/ipv4host/ipv4alias1", KDB_O_NONE);
95 2 : succeed_if (key, "ipv4alias1 not found");
96 2 : key = ksLookupByName (ks, "user/tests/hosts/ipv4/ipv4host/ipv4alias2", KDB_O_NONE);
97 2 : succeed_if (key, "ipv4alias2 not found");
98 :
99 2 : key = ksLookupByName (ks, "user/tests/hosts/ipv6/ipv6host", KDB_O_NONE);
100 2 : exit_if_fail (key, "hostname ipv6host not found");
101 2 : succeed_if (strcmp ("::1", keyValue (key)) == 0, "address not correct");
102 :
103 2 : key = ksLookupByName (ks, "user/tests/hosts/ipv6/ipv6host/ipv6alias1", KDB_O_NONE);
104 2 : succeed_if (key, "ipv6alias1 not found");
105 2 : key = ksLookupByName (ks, "user/tests/hosts/ipv6/ipv6host/ipv6alias2", KDB_O_NONE);
106 2 : succeed_if (key, "ipv6alias2 not found");
107 :
108 2 : ksDel (ks);
109 2 : keyDel (parentKey);
110 :
111 2 : PLUGIN_CLOSE ();
112 2 : }
113 :
114 2 : void test_duplicateEntries (char * fileName)
115 : {
116 2 : Key * parentKey = keyNew ("user/tests/hosts", KEY_VALUE, srcdir_file (fileName), KEY_END);
117 2 : KeySet * conf = 0;
118 2 : PLUGIN_OPEN ("hosts");
119 :
120 2 : KeySet * ks = ksNew (0, KS_END);
121 :
122 2 : succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
123 :
124 2 : Key * key = ksLookupByName (ks, "user/tests/hosts/ipv4/localhost", KDB_O_NONE);
125 2 : exit_if_fail (key, "hostname localhost not found");
126 2 : succeed_if (strcmp ("127.0.0.1", keyValue (key)) == 0, "address not correct");
127 :
128 2 : key = ksLookupByName (ks, "user/tests/hosts/ipv4/host", KDB_O_NONE);
129 2 : exit_if_fail (key, "hostname host not found");
130 2 : succeed_if (strcmp ("192.168.0.1", keyValue (key)) == 0, "address not correct");
131 :
132 2 : key = ksLookupByName (ks, "user/tests/hosts/ipv4/host/alias1", KDB_O_NONE);
133 2 : succeed_if (key, "alias1 not found");
134 2 : key = ksLookupByName (ks, "user/tests/hosts/ipv4/host/alias2", KDB_O_NONE);
135 2 : succeed_if (key, "alias2 not found");
136 :
137 2 : ksDel (ks);
138 2 : keyDel (parentKey);
139 :
140 2 : PLUGIN_CLOSE ();
141 2 : }
142 :
143 2 : void test_duplicateOrder (char * fileName)
144 : {
145 2 : Key * parentKey = keyNew ("user/tests/hosts", KEY_VALUE, elektraFilename (), KEY_END);
146 2 : KeySet * conf = 0;
147 2 : PLUGIN_OPEN ("hosts");
148 :
149 : // clang-format off
150 2 : KeySet *ks = ksNew (20,
151 : keyNew ("user/tests/hosts/ipv4/host1",
152 : KEY_VALUE, "192.168.0.1",
153 : KEY_META, "order", "10",
154 : KEY_END),
155 : keyNew ("user/tests/hosts/ipv4/host2",
156 : KEY_VALUE, "192.168.0.2",
157 : KEY_META, "order", "20",
158 : KEY_END),
159 : keyNew ("user/tests/hosts/ipv4/host3",
160 : KEY_VALUE, "192.168.0.3",
161 : KEY_META, "order", "20",
162 : KEY_END),
163 : KS_END);
164 : // clang-format on
165 :
166 2 : ksAppendKey (ks, parentKey);
167 :
168 2 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
169 2 : succeed_if (output_error (parentKey), "error in kdbSet");
170 2 : succeed_if (output_warnings (parentKey), "warnings in kdbSet");
171 :
172 2 : succeed_if (compare_line_files (srcdir_file (fileName), keyString (parentKey)), "files do not match as expected");
173 :
174 2 : elektraUnlink (keyString (parentKey));
175 2 : ksDel (ks);
176 :
177 2 : PLUGIN_CLOSE ();
178 2 : }
179 :
180 2 : void test_writeHostsSimple (char * fileName)
181 : {
182 2 : Key * parentKey = keyNew ("user/tests/hosts", KEY_VALUE, elektraFilename (), KEY_END);
183 2 : KeySet * conf = 0;
184 2 : PLUGIN_OPEN ("hosts");
185 :
186 : // clang-format off
187 2 : KeySet *ks = ksNew (20,
188 : keyNew ("user/tests/hosts/ipv4/localhost",
189 : KEY_VALUE, "127.0.0.1",
190 : KEY_META, "order", "10",
191 : KEY_META, "comment/#0", "",
192 : KEY_META, "comment/#1", " these are for ipv4",
193 : KEY_META, "comment/#1/start", "#",
194 : KEY_END),
195 : keyNew ("user/tests/hosts/ipv4/testhost",
196 : KEY_VALUE, "127.0.1.1",
197 : KEY_META, "order", "20",
198 : KEY_END),
199 : keyNew ("user/tests/hosts/ipv4/testhost/testhostalias",
200 : KEY_END),
201 : keyNew ("user/tests/hosts/ipv6/localhost",
202 : KEY_VALUE, "::1",
203 : KEY_META, "order", "30",
204 : KEY_META, "comment/#0", "",
205 : KEY_META, "comment/#1", " The following lines are desirable for IPv6 capable hosts",
206 : KEY_META, "comment/#1/start", "#",
207 : KEY_END),
208 : keyNew ("user/tests/hosts/ipv6/localhost/ip6-localhost",
209 : KEY_END),
210 : keyNew ("user/tests/hosts/ipv6/localhost/ip6-loopback",
211 : KEY_END),
212 : keyNew ("user/tests/hosts/ipv6/ip6-allnodes",
213 : KEY_VALUE, "ff02::1",
214 : KEY_META, "order", "40",
215 : KEY_END),
216 : KS_END);
217 : // clang-format on
218 :
219 2 : ksAppendKey (ks, parentKey);
220 :
221 2 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
222 2 : succeed_if (output_error (parentKey), "error in kdbSet");
223 2 : succeed_if (output_warnings (parentKey), "warnings in kdbSet");
224 :
225 2 : succeed_if (compare_line_files (srcdir_file (fileName), keyString (parentKey)), "files do not match as expected");
226 :
227 2 : elektraUnlink (keyString (parentKey));
228 2 : ksDel (ks);
229 :
230 2 : PLUGIN_CLOSE ();
231 2 : }
232 :
233 2 : void test_readHostsComments (char * fileName)
234 : {
235 2 : Key * parentKey = keyNew ("user/tests/hosts", KEY_VALUE, srcdir_file (fileName), KEY_END);
236 2 : KeySet * conf = 0;
237 2 : PLUGIN_OPEN ("hosts");
238 :
239 2 : KeySet * ks = ksNew (0, KS_END);
240 :
241 2 : succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
242 :
243 : /* FIRST ENTRY */
244 2 : Key * key = ksLookupByName (ks, "user/tests/hosts/ipv4/localhost", 0);
245 2 : exit_if_fail (key, "hostname localhost not found");
246 :
247 : /* inline comment */
248 2 : const Key * inlineComment1 = keyGetMeta (key, "comment/#0");
249 2 : succeed_if (inlineComment1, "inline comment for first host does not exist");
250 2 : succeed_if (!strcmp (keyString (inlineComment1), "inline comment0"), "inline comment for first host contains wrong text");
251 :
252 2 : const Key * inlineComment1Start = keyGetMeta (key, "comment/#0/start");
253 2 : succeed_if (inlineComment1Start, "start key for inline of first host does not exist");
254 2 : succeed_if (!strcmp (keyString (inlineComment1Start), "#"), "start key for inline comment of first host contains wrong text");
255 :
256 2 : const Key * inlineComment1Space = keyGetMeta (key, "comment/#0/space");
257 2 : succeed_if (inlineComment1Space, "space key for inline comment of first host does not exist");
258 2 : succeed_if (!strcmp (keyString (inlineComment1Space), "3"),
259 : "space key for inline comment of first host contains wrong number of spaces");
260 :
261 :
262 : /* empty lines */
263 2 : const Key * lineComment1 = keyGetMeta (key, "comment/#1");
264 2 : succeed_if (lineComment1, "comment for first empty line does not exist");
265 :
266 2 : const Key * lineComment2 = keyGetMeta (key, "comment/#2");
267 2 : succeed_if (lineComment2, "comment for second empty line does not exist");
268 :
269 :
270 : /* line comment */
271 2 : const Key * lineComment3 = keyGetMeta (key, "comment/#3");
272 2 : succeed_if (lineComment3, "comment key for line comment does not exist");
273 2 : succeed_if (!strcmp (keyString (lineComment3), " comment for localhost"), "comment key for line comment contains wrong text");
274 :
275 2 : const Key * lineComment3Start = keyGetMeta (key, "comment/#3/start");
276 2 : succeed_if (lineComment3Start, "start key for line comment does not exist");
277 2 : succeed_if (!strcmp (keyString (lineComment3Start), "#"), "start key for line comment contains wrong text");
278 :
279 2 : const Key * lineComment3Spaces = keyGetMeta (key, "comment/#3/space");
280 2 : succeed_if (lineComment3Spaces, "space key for line comment does not exist");
281 2 : succeed_if (!strcmp (keyString (lineComment3Spaces), "0"), "space key for line comment contains wrong number of spaces");
282 :
283 : /* empty line */
284 2 : const Key * emptyLine = keyGetMeta (key, "comment/#4");
285 2 : succeed_if (emptyLine, "comment key for line comment does not exist");
286 2 : succeed_if (!strcmp ("", keyString (emptyLine)), "line comment key contains data although it shouldn't")
287 :
288 : /* SECOND ENTRY */
289 2 : Key * key2 = ksLookupByName (ks, "user/tests/hosts/ipv4/testentry", 0);
290 2 : exit_if_fail (key2, "hostname localhost not found");
291 :
292 : /* inline comment */
293 2 : const Key * inlineComment2 = keyGetMeta (key2, "comment/#0");
294 2 : succeed_if (inlineComment2, "inline comment for second host does not exist");
295 2 : succeed_if (!strcmp (keyString (inlineComment2), " inline comment1"), "inline comment for second host contains wrong text");
296 :
297 2 : const Key * inlineComment2Start = keyGetMeta (key2, "comment/#0/start");
298 2 : succeed_if (inlineComment2Start, "start key for inline of second host does not exist");
299 2 : succeed_if (!strcmp (keyString (inlineComment2Start), "#"), "start key for inline comment of second host contains wrong text");
300 :
301 2 : const Key * inlineComment2Space = keyGetMeta (key2, "comment/#0/space");
302 2 : succeed_if (inlineComment2Space, "space key for inline comment of second host does not exist");
303 2 : succeed_if (!strcmp (keyString (inlineComment2Space), "1"),
304 : "space key for inline comment of second host contains wrong number of spaces");
305 :
306 :
307 : /* empty line */
308 2 : const Key * lineComment4 = keyGetMeta (key2, "comment/#1");
309 2 : succeed_if (lineComment4, "comment key for line comment does not exist");
310 :
311 2 : const Key * lineComment4Spaces = keyGetMeta (key2, "comment/#1/space");
312 2 : succeed_if (lineComment4Spaces, "space key for line comment does not exist");
313 2 : succeed_if (!strcmp (keyString (lineComment4Spaces), "2"), "space key for line comment contains wrong number of spaces");
314 :
315 :
316 : /* line comment */
317 2 : const Key * lineComment5 = keyGetMeta (key2, "comment/#2");
318 2 : succeed_if (lineComment5, "comment key for line comment does not exist");
319 2 : succeed_if (!strcmp (keyString (lineComment5), " comment for testentry"), "comment key for line comment contains wrong text");
320 :
321 2 : const Key * lineComment5Start = keyGetMeta (key2, "comment/#2/start");
322 2 : succeed_if (lineComment5Start, "start key for line comment does not exist");
323 2 : succeed_if (!strcmp (keyString (lineComment5Start), "#"), "start key for line comment contains wrong text");
324 :
325 2 : const Key * lineComment5Spaces = keyGetMeta (key2, "comment/#2/space");
326 2 : succeed_if (lineComment5Spaces, "space key for line comment does not exist");
327 2 : succeed_if (!strcmp (keyString (lineComment5Spaces), "2"), "space key for line comment contains wrong number of spaces");
328 :
329 :
330 : /* NO ENTRY */
331 :
332 : /* empty line */
333 2 : const Key * lineComment6 = keyGetMeta (parentKey, "comment/#1");
334 2 : succeed_if (lineComment6, "comment key for line comment does not exist");
335 :
336 :
337 : /* line comment */
338 2 : const Key * lineComment7 = keyGetMeta (parentKey, "comment/#2");
339 2 : succeed_if (lineComment7, "comment key for line comment does not exist");
340 2 : succeed_if (!strcmp (keyString (lineComment7), " comment without entry"), "comment key for line comment contains wrong text");
341 :
342 2 : const Key * lineComment7Start = keyGetMeta (parentKey, "comment/#2/start");
343 2 : succeed_if (lineComment7Start, "start key for line comment does not exist");
344 2 : succeed_if (!strcmp (keyString (lineComment7Start), "#"), "start key for line comment contains wrong text");
345 :
346 2 : const Key * lineComment7Spaces = keyGetMeta (parentKey, "comment/#2/space");
347 2 : succeed_if (lineComment7Spaces, "space key for line comment does not exist");
348 2 : succeed_if (!strcmp (keyString (lineComment7Spaces), "0"), "space key for line comment contains wrong number of spaces");
349 :
350 :
351 2 : ksDel (ks);
352 2 : keyDel (parentKey);
353 :
354 2 : PLUGIN_CLOSE ();
355 2 : }
356 :
357 2 : void test_writeHostsComments (char * fileName)
358 : {
359 2 : Key * parentKey = keyNew ("user/tests/hosts", KEY_VALUE, elektraFilename (), KEY_END);
360 2 : KeySet * conf = 0;
361 2 : PLUGIN_OPEN ("hosts");
362 :
363 : // clang-format off
364 2 : KeySet *ks = ksNew (20,
365 : keyNew ("user/tests/hosts/ipv4/localhost",
366 : KEY_VALUE, "127.0.0.1",
367 : KEY_META, "order", "10",
368 : KEY_META, "comment/#0", "inline comment0",
369 : KEY_META, "comment/#0/space", "3",
370 : KEY_META, "comment/#0/start", "#",
371 : KEY_META, "comment/#1", "",
372 : KEY_META, "comment/#2", "",
373 : KEY_META, "comment/#3", " comment for localhost",
374 : KEY_META, "comment/#3/start", "#",
375 : KEY_META, "comment/#4", "",
376 : KEY_END),
377 : keyNew ("user/tests/hosts/ipv4/testentry",
378 : KEY_VALUE, "192.168.0.1",
379 : KEY_META, "order", "20",
380 : KEY_META, "comment/#0", " inline comment1",
381 : KEY_META, "comment/#0/space", "1",
382 : KEY_META, "comment/#0/start", "#",
383 : KEY_META, "comment/#1", "",
384 : KEY_META, "comment/#1/space", "2",
385 : KEY_META, "comment/#2", " comment for testentry",
386 : KEY_META, "comment/#2/space", "2",
387 : KEY_META, "comment/#2/start", "#",
388 : KEY_END),
389 : keyNew ("user/tests/hosts/ipv4/testentry/alias1",
390 : KEY_END),
391 : keyNew ("user/tests/hosts/ipv4/testentry/alias2",
392 : KEY_END),
393 : KS_END);
394 : // clang-format on
395 :
396 2 : keySetMeta (parentKey, "comment/#1", "");
397 2 : keySetMeta (parentKey, "comment/#2", " comment without entry");
398 2 : keySetMeta (parentKey, "comment/#2/start", "#");
399 :
400 2 : ksAppendKey (ks, parentKey);
401 :
402 2 : succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
403 2 : succeed_if (output_error (parentKey), "error in kdbSet");
404 2 : succeed_if (output_warnings (parentKey), "warnings in kdbSet");
405 :
406 2 : succeed_if (compare_line_files (srcdir_file (fileName), keyString (parentKey)), "files do not match as expected");
407 :
408 2 : elektraUnlink (keyString (parentKey));
409 2 : ksDel (ks);
410 :
411 2 : PLUGIN_CLOSE ();
412 2 : }
413 :
414 2 : int main (int argc, char ** argv)
415 : {
416 2 : printf ("MOUNT TESTS\n");
417 2 : printf ("==================\n\n");
418 :
419 2 : init (argc, argv);
420 :
421 2 : test_readHostsSimple ("hosts/hosts-read-simple");
422 2 : test_readInvalidIpAddress ("hosts/hosts-invalid");
423 2 : test_mixedAddresses ("hosts/hosts-mixed");
424 2 : test_duplicateEntries ("hosts/hosts-duplicate");
425 2 : test_duplicateOrder ("hosts/hosts-duporder");
426 2 : test_writeHostsSimple ("hosts/hosts-write-simple");
427 2 : test_readHostsComments ("hosts/hosts-comments");
428 2 : test_writeHostsComments ("hosts/hosts-comments");
429 :
430 2 : print_result ("test_hosts");
431 :
432 2 : return nbError;
433 : }
|