forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testmod_hosts.c
433 lines (334 loc) · 15.7 KB
/
testmod_hosts.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/**
* @file
*
* @brief Tests for the hosts plugin
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#ifdef HAVE_KDBCONFIG_H
#include "kdbconfig.h"
#endif
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <tests_plugin.h>
void test_readHostsSimple (char * fileName)
{
Key * parentKey = keyNew ("user/tests/hosts", KEY_VALUE, srcdir_file (fileName), KEY_END);
KeySet * conf = 0;
PLUGIN_OPEN ("hosts");
KeySet * ks = ksNew (0, KS_END);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
Key * key = ksLookupByName (ks, "user/tests/hosts/ipv4/localhost", 0);
exit_if_fail (key, "hostname localhost not found");
succeed_if (strcmp ("127.0.0.1", keyValue (key)) == 0, "address not correct");
key = ksLookupByName (ks, "user/tests/hosts/ipv4/gateway.markus-raab.org", 0);
exit_if_fail (key, "hostname gateway.markus-raab.org not found");
succeed_if (strcmp ("192.168.0.1", keyValue (key)) == 0, "address not correct");
key = ksLookupByName (ks, "user/tests/hosts/ipv4/kirabyte.markus-raab.org/kira", 0);
exit_if_fail (key, "hostname alias kira not found");
succeed_if (strcmp ("192.168.0.5", keyValue (key)) == 0, "address not correct");
key = ksLookupByName (ks, "user/tests/hosts/ipv6/wikipedia-sample", 0);
exit_if_fail (key, "hostname wikipedia-sample not found");
succeed_if (strcmp ("fd9e:21a7:a92c:2323::1", keyValue (key)) == 0, "address not correct");
key = ksLookupByName (ks, "user/tests/hosts/ipv6/wikipedia-sample/wikipedia-alias", 0);
exit_if_fail (key, "hostname alias wikipedia-alias not found");
succeed_if (strcmp ("fd9e:21a7:a92c:2323::1", keyValue (key)) == 0, "address not correct");
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
void test_readInvalidIpAddress (char * fileName)
{
Key * parentKey = keyNew ("user/tests/hosts", KEY_VALUE, srcdir_file (fileName), KEY_END);
KeySet * conf = 0;
PLUGIN_OPEN ("hosts");
KeySet * ks = ksNew (0, KS_END);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
Key * key = ksLookupByName (ks, "user/tests/hosts/ipv4/localhost", KDB_O_NONE);
exit_if_fail (key, "hostname localhost not found");
succeed_if (strcmp ("noipaddress", keyValue (key)) == 0, "address not correct");
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
void test_mixedAddresses (char * fileName)
{
Key * parentKey = keyNew ("user/tests/hosts", KEY_VALUE, srcdir_file (fileName), KEY_END);
KeySet * conf = 0;
PLUGIN_OPEN ("hosts");
KeySet * ks = ksNew (0, KS_END);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
Key * key = ksLookupByName (ks, "user/tests/hosts/ipv4/ipv4host", KDB_O_NONE);
exit_if_fail (key, "hostname ipv4host not found");
succeed_if (strcmp ("127.0.0.1", keyValue (key)) == 0, "address not correct");
key = ksLookupByName (ks, "user/tests/hosts/ipv4/ipv4host/ipv4alias1", KDB_O_NONE);
succeed_if (key, "ipv4alias1 not found");
key = ksLookupByName (ks, "user/tests/hosts/ipv4/ipv4host/ipv4alias2", KDB_O_NONE);
succeed_if (key, "ipv4alias2 not found");
key = ksLookupByName (ks, "user/tests/hosts/ipv6/ipv6host", KDB_O_NONE);
exit_if_fail (key, "hostname ipv6host not found");
succeed_if (strcmp ("::1", keyValue (key)) == 0, "address not correct");
key = ksLookupByName (ks, "user/tests/hosts/ipv6/ipv6host/ipv6alias1", KDB_O_NONE);
succeed_if (key, "ipv6alias1 not found");
key = ksLookupByName (ks, "user/tests/hosts/ipv6/ipv6host/ipv6alias2", KDB_O_NONE);
succeed_if (key, "ipv6alias2 not found");
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
void test_duplicateEntries (char * fileName)
{
Key * parentKey = keyNew ("user/tests/hosts", KEY_VALUE, srcdir_file (fileName), KEY_END);
KeySet * conf = 0;
PLUGIN_OPEN ("hosts");
KeySet * ks = ksNew (0, KS_END);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
Key * key = ksLookupByName (ks, "user/tests/hosts/ipv4/localhost", KDB_O_NONE);
exit_if_fail (key, "hostname localhost not found");
succeed_if (strcmp ("127.0.0.1", keyValue (key)) == 0, "address not correct");
key = ksLookupByName (ks, "user/tests/hosts/ipv4/host", KDB_O_NONE);
exit_if_fail (key, "hostname host not found");
succeed_if (strcmp ("192.168.0.1", keyValue (key)) == 0, "address not correct");
key = ksLookupByName (ks, "user/tests/hosts/ipv4/host/alias1", KDB_O_NONE);
succeed_if (key, "alias1 not found");
key = ksLookupByName (ks, "user/tests/hosts/ipv4/host/alias2", KDB_O_NONE);
succeed_if (key, "alias2 not found");
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
void test_duplicateOrder (char * fileName)
{
Key * parentKey = keyNew ("user/tests/hosts", KEY_VALUE, elektraFilename (), KEY_END);
KeySet * conf = 0;
PLUGIN_OPEN ("hosts");
// clang-format off
KeySet *ks = ksNew (20,
keyNew ("user/tests/hosts/ipv4/host1",
KEY_VALUE, "192.168.0.1",
KEY_META, "order", "10",
KEY_END),
keyNew ("user/tests/hosts/ipv4/host2",
KEY_VALUE, "192.168.0.2",
KEY_META, "order", "20",
KEY_END),
keyNew ("user/tests/hosts/ipv4/host3",
KEY_VALUE, "192.168.0.3",
KEY_META, "order", "20",
KEY_END),
KS_END);
// clang-format on
ksAppendKey (ks, parentKey);
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (output_error (parentKey), "error in kdbSet");
succeed_if (output_warnings (parentKey), "warnings in kdbSet");
succeed_if (compare_line_files (srcdir_file (fileName), keyString (parentKey)), "files do not match as expected");
elektraUnlink (keyString (parentKey));
ksDel (ks);
PLUGIN_CLOSE ();
}
void test_writeHostsSimple (char * fileName)
{
Key * parentKey = keyNew ("user/tests/hosts", KEY_VALUE, elektraFilename (), KEY_END);
KeySet * conf = 0;
PLUGIN_OPEN ("hosts");
// clang-format off
KeySet *ks = ksNew (20,
keyNew ("user/tests/hosts/ipv4/localhost",
KEY_VALUE, "127.0.0.1",
KEY_META, "order", "10",
KEY_META, "comment/#0", "",
KEY_META, "comment/#1", " these are for ipv4",
KEY_META, "comment/#1/start", "#",
KEY_END),
keyNew ("user/tests/hosts/ipv4/testhost",
KEY_VALUE, "127.0.1.1",
KEY_META, "order", "20",
KEY_END),
keyNew ("user/tests/hosts/ipv4/testhost/testhostalias",
KEY_END),
keyNew ("user/tests/hosts/ipv6/localhost",
KEY_VALUE, "::1",
KEY_META, "order", "30",
KEY_META, "comment/#0", "",
KEY_META, "comment/#1", " The following lines are desirable for IPv6 capable hosts",
KEY_META, "comment/#1/start", "#",
KEY_END),
keyNew ("user/tests/hosts/ipv6/localhost/ip6-localhost",
KEY_END),
keyNew ("user/tests/hosts/ipv6/localhost/ip6-loopback",
KEY_END),
keyNew ("user/tests/hosts/ipv6/ip6-allnodes",
KEY_VALUE, "ff02::1",
KEY_META, "order", "40",
KEY_END),
KS_END);
// clang-format on
ksAppendKey (ks, parentKey);
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (output_error (parentKey), "error in kdbSet");
succeed_if (output_warnings (parentKey), "warnings in kdbSet");
succeed_if (compare_line_files (srcdir_file (fileName), keyString (parentKey)), "files do not match as expected");
elektraUnlink (keyString (parentKey));
ksDel (ks);
PLUGIN_CLOSE ();
}
void test_readHostsComments (char * fileName)
{
Key * parentKey = keyNew ("user/tests/hosts", KEY_VALUE, srcdir_file (fileName), KEY_END);
KeySet * conf = 0;
PLUGIN_OPEN ("hosts");
KeySet * ks = ksNew (0, KS_END);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) >= 1, "call to kdbGet was not successful");
/* FIRST ENTRY */
Key * key = ksLookupByName (ks, "user/tests/hosts/ipv4/localhost", 0);
exit_if_fail (key, "hostname localhost not found");
/* inline comment */
const Key * inlineComment1 = keyGetMeta (key, "comment/#0");
succeed_if (inlineComment1, "inline comment for first host does not exist");
succeed_if (!strcmp (keyString (inlineComment1), "inline comment0"), "inline comment for first host contains wrong text");
const Key * inlineComment1Start = keyGetMeta (key, "comment/#0/start");
succeed_if (inlineComment1Start, "start key for inline of first host does not exist");
succeed_if (!strcmp (keyString (inlineComment1Start), "#"), "start key for inline comment of first host contains wrong text");
const Key * inlineComment1Space = keyGetMeta (key, "comment/#0/space");
succeed_if (inlineComment1Space, "space key for inline comment of first host does not exist");
succeed_if (!strcmp (keyString (inlineComment1Space), "3"),
"space key for inline comment of first host contains wrong number of spaces");
/* empty lines */
const Key * lineComment1 = keyGetMeta (key, "comment/#1");
succeed_if (lineComment1, "comment for first empty line does not exist");
const Key * lineComment2 = keyGetMeta (key, "comment/#2");
succeed_if (lineComment2, "comment for second empty line does not exist");
/* line comment */
const Key * lineComment3 = keyGetMeta (key, "comment/#3");
succeed_if (lineComment3, "comment key for line comment does not exist");
succeed_if (!strcmp (keyString (lineComment3), " comment for localhost"), "comment key for line comment contains wrong text");
const Key * lineComment3Start = keyGetMeta (key, "comment/#3/start");
succeed_if (lineComment3Start, "start key for line comment does not exist");
succeed_if (!strcmp (keyString (lineComment3Start), "#"), "start key for line comment contains wrong text");
const Key * lineComment3Spaces = keyGetMeta (key, "comment/#3/space");
succeed_if (lineComment3Spaces, "space key for line comment does not exist");
succeed_if (!strcmp (keyString (lineComment3Spaces), "0"), "space key for line comment contains wrong number of spaces");
/* empty line */
const Key * emptyLine = keyGetMeta (key, "comment/#4");
succeed_if (emptyLine, "comment key for line comment does not exist");
succeed_if (!strcmp ("", keyString (emptyLine)), "line comment key contains data although it shouldn't")
/* SECOND ENTRY */
Key * key2 = ksLookupByName (ks, "user/tests/hosts/ipv4/testentry", 0);
exit_if_fail (key2, "hostname localhost not found");
/* inline comment */
const Key * inlineComment2 = keyGetMeta (key2, "comment/#0");
succeed_if (inlineComment2, "inline comment for second host does not exist");
succeed_if (!strcmp (keyString (inlineComment2), " inline comment1"), "inline comment for second host contains wrong text");
const Key * inlineComment2Start = keyGetMeta (key2, "comment/#0/start");
succeed_if (inlineComment2Start, "start key for inline of second host does not exist");
succeed_if (!strcmp (keyString (inlineComment2Start), "#"), "start key for inline comment of second host contains wrong text");
const Key * inlineComment2Space = keyGetMeta (key2, "comment/#0/space");
succeed_if (inlineComment2Space, "space key for inline comment of second host does not exist");
succeed_if (!strcmp (keyString (inlineComment2Space), "1"),
"space key for inline comment of second host contains wrong number of spaces");
/* empty line */
const Key * lineComment4 = keyGetMeta (key2, "comment/#1");
succeed_if (lineComment4, "comment key for line comment does not exist");
const Key * lineComment4Spaces = keyGetMeta (key2, "comment/#1/space");
succeed_if (lineComment4Spaces, "space key for line comment does not exist");
succeed_if (!strcmp (keyString (lineComment4Spaces), "2"), "space key for line comment contains wrong number of spaces");
/* line comment */
const Key * lineComment5 = keyGetMeta (key2, "comment/#2");
succeed_if (lineComment5, "comment key for line comment does not exist");
succeed_if (!strcmp (keyString (lineComment5), " comment for testentry"), "comment key for line comment contains wrong text");
const Key * lineComment5Start = keyGetMeta (key2, "comment/#2/start");
succeed_if (lineComment5Start, "start key for line comment does not exist");
succeed_if (!strcmp (keyString (lineComment5Start), "#"), "start key for line comment contains wrong text");
const Key * lineComment5Spaces = keyGetMeta (key2, "comment/#2/space");
succeed_if (lineComment5Spaces, "space key for line comment does not exist");
succeed_if (!strcmp (keyString (lineComment5Spaces), "2"), "space key for line comment contains wrong number of spaces");
/* NO ENTRY */
/* empty line */
const Key * lineComment6 = keyGetMeta (parentKey, "comment/#1");
succeed_if (lineComment6, "comment key for line comment does not exist");
/* line comment */
const Key * lineComment7 = keyGetMeta (parentKey, "comment/#2");
succeed_if (lineComment7, "comment key for line comment does not exist");
succeed_if (!strcmp (keyString (lineComment7), " comment without entry"), "comment key for line comment contains wrong text");
const Key * lineComment7Start = keyGetMeta (parentKey, "comment/#2/start");
succeed_if (lineComment7Start, "start key for line comment does not exist");
succeed_if (!strcmp (keyString (lineComment7Start), "#"), "start key for line comment contains wrong text");
const Key * lineComment7Spaces = keyGetMeta (parentKey, "comment/#2/space");
succeed_if (lineComment7Spaces, "space key for line comment does not exist");
succeed_if (!strcmp (keyString (lineComment7Spaces), "0"), "space key for line comment contains wrong number of spaces");
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
void test_writeHostsComments (char * fileName)
{
Key * parentKey = keyNew ("user/tests/hosts", KEY_VALUE, elektraFilename (), KEY_END);
KeySet * conf = 0;
PLUGIN_OPEN ("hosts");
// clang-format off
KeySet *ks = ksNew (20,
keyNew ("user/tests/hosts/ipv4/localhost",
KEY_VALUE, "127.0.0.1",
KEY_META, "order", "10",
KEY_META, "comment/#0", "inline comment0",
KEY_META, "comment/#0/space", "3",
KEY_META, "comment/#0/start", "#",
KEY_META, "comment/#1", "",
KEY_META, "comment/#2", "",
KEY_META, "comment/#3", " comment for localhost",
KEY_META, "comment/#3/start", "#",
KEY_META, "comment/#4", "",
KEY_END),
keyNew ("user/tests/hosts/ipv4/testentry",
KEY_VALUE, "192.168.0.1",
KEY_META, "order", "20",
KEY_META, "comment/#0", " inline comment1",
KEY_META, "comment/#0/space", "1",
KEY_META, "comment/#0/start", "#",
KEY_META, "comment/#1", "",
KEY_META, "comment/#1/space", "2",
KEY_META, "comment/#2", " comment for testentry",
KEY_META, "comment/#2/space", "2",
KEY_META, "comment/#2/start", "#",
KEY_END),
keyNew ("user/tests/hosts/ipv4/testentry/alias1",
KEY_END),
keyNew ("user/tests/hosts/ipv4/testentry/alias2",
KEY_END),
KS_END);
// clang-format on
keySetMeta (parentKey, "comment/#1", "");
keySetMeta (parentKey, "comment/#2", " comment without entry");
keySetMeta (parentKey, "comment/#2/start", "#");
ksAppendKey (ks, parentKey);
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == 1, "kdbSet was not successful");
succeed_if (output_error (parentKey), "error in kdbSet");
succeed_if (output_warnings (parentKey), "warnings in kdbSet");
succeed_if (compare_line_files (srcdir_file (fileName), keyString (parentKey)), "files do not match as expected");
elektraUnlink (keyString (parentKey));
ksDel (ks);
PLUGIN_CLOSE ();
}
int main (int argc, char ** argv)
{
printf ("MOUNT TESTS\n");
printf ("==================\n\n");
init (argc, argv);
test_readHostsSimple ("hosts/hosts-read-simple");
test_readInvalidIpAddress ("hosts/hosts-invalid");
test_mixedAddresses ("hosts/hosts-mixed");
test_duplicateEntries ("hosts/hosts-duplicate");
test_duplicateOrder ("hosts/hosts-duporder");
test_writeHostsSimple ("hosts/hosts-write-simple");
test_readHostsComments ("hosts/hosts-comments");
test_writeHostsComments ("hosts/hosts-comments");
print_result ("test_hosts");
return nbError;
}