forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testmod_blacklist.c
154 lines (125 loc) · 6.34 KB
/
testmod_blacklist.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
/**
* @file
*
* @brief
*
* @copyright BSD License (see LICENSE.md or https://www.libelektra.org)
*
*/
#include <kdbmodule.h>
#include <kdbplugin.h>
#include <tests_plugin.h>
static void test_blacklist (void)
{
Key * parentKey = keyNew ("user:/tests/blacklist", KEY_VALUE, "", KEY_END);
Key * k1 = keyNew ("user:/tests/blacklist/valid1", KEY_VALUE, "ICE", KEY_META, "check/blacklist", "#1", KEY_META,
"check/blacklist/#0", "FIRE", KEY_META, "check/blacklist/#1", "EARTH", KEY_END);
Key * k2 = keyNew ("user:/tests/blacklist/valid2", KEY_VALUE, "EARTH", KEY_META, "check/blacklist", "#2", KEY_META,
"check/blacklist/#0", "FIRE", KEY_META, "check/blacklist/#2", "WATER", KEY_END);
Key * k3 = keyNew ("user:/tests/blacklist/valid3", KEY_VALUE, "", KEY_META, "check/blacklist", "#1", KEY_META, "check/blacklist/#0",
"FIRE", KEY_META, "check/blacklist/#1", "EARTH", KEY_END);
Key * k4 = keyNew ("user:/tests/blacklist/valid4", KEY_VALUE, "WATER", KEY_META, "check/blacklist", "#0", KEY_META,
"check/blacklist/#0", "COLD/WATER", KEY_END);
Key * k5 = keyNew ("user:/tests/blacklist/invalid1", KEY_VALUE, "ICE", KEY_META, "check/blacklist", "#2", KEY_META,
"check/blacklist/#0", "FIRE", KEY_META, "check/blacklist/#1", "EARTH", KEY_META, "check/blacklist/#2", "ICE",
KEY_END);
Key * k6 = keyNew ("user:/tests/blacklist/invalid2", KEY_VALUE, "FIRE", KEY_META, "check/blacklist", "#0", KEY_META,
"check/blacklist/#0", "FIRE", KEY_END);
Key * k7 = keyNew ("user:/tests/blacklist/invalid3", KEY_VALUE, "COLD/WATER", KEY_META, "check/blacklist", "#0", KEY_META,
"check/blacklist/#0", "COLD/WATER", KEY_END);
KeySet * conf = ksNew (0, KS_END);
KeySet * ks = ksNew (4, k1, k2, k3, k4, KS_END);
PLUGIN_OPEN ("blacklist");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "kdbGet failed");
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "kdbSet failed");
ksDel (ks);
ks = ksNew (20, KS_END);
ksAppendKey (ks, k5);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_ERROR, "kdbGet should have failed");
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_ERROR, "kdbSet should have failed");
ksDel (ks);
ks = ksNew (20, KS_END);
ksAppendKey (ks, k6);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_ERROR, "kdbGet should have failed");
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_ERROR, "kdbSet should have failed");
ksDel (ks);
ks = ksNew (20, KS_END);
ksAppendKey (ks, k7);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_ERROR, "kdbGet should have failed");
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_ERROR, "kdbSet should have failed");
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
static void test_blacklist_empty_values (void)
{
Key * parentKey = keyNew ("user:/tests/blacklist", KEY_VALUE, "", KEY_END);
// empty blacklist value
Key * k1 = keyNew ("user:/tests/blacklist/valid1", KEY_VALUE, "ICE", KEY_META, "check/blacklist", "#0", KEY_META,
"check/blacklist/#0", "", KEY_END);
Key * k2 = keyNew ("user:/tests/blacklist/invalid1", KEY_VALUE, "", KEY_META, "check/blacklist", "#0", KEY_META,
"check/blacklist/#0", "", KEY_END);
// empty blacklist array - should not blacklist anything
Key * k3 = keyNew ("user:/tests/blacklist/valid2", KEY_VALUE, "", KEY_META, "check/blacklist", "", KEY_END);
// no blacklist meta-keys provided
Key * k4 = keyNew ("user:/tests/blacklist/valid3", KEY_VALUE, "ICE", KEY_END);
KeySet * conf = ksNew (0, KS_END);
KeySet * ks = ksNew (1, k1, KS_END);
PLUGIN_OPEN ("blacklist");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "kdbGet failed");
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "kdbSet failed");
ksDel (ks);
ks = ksNew (20, KS_END);
ksAppendKey (ks, k2);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_ERROR, "kdbGet should have failed");
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_ERROR, "kdbSet should have failed");
ksDel (ks);
ks = ksNew (20, KS_END);
ksAppendKey (ks, k3);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "kdbGet should have failed");
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "kdbSet should have failed");
ksDel (ks);
ks = ksNew (20, KS_END);
ksAppendKey (ks, k4);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "kdbGet failed");
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "kdbSet failed");
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
static void test_blacklist_null_values (void)
{
Key * parentKey = keyNew ("user:/tests/blacklist", KEY_VALUE, "", KEY_END);
Key * k1 = keyNew ("user:/tests/blacklist/valid1", KEY_VALUE, "", KEY_END);
Key * k2 = keyNew ("user:/tests/blacklist/valid2", KEY_VALUE, NULL, KEY_END);
Key * k3 = keyNew ("user:/tests/blacklist/valid3", KEY_VALUE, NULL, KEY_META, "check/blacklist", "", KEY_END);
KeySet * conf = ksNew (0, KS_END);
KeySet * ks = ksNew (1, k1, KS_END);
PLUGIN_OPEN ("blacklist");
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "kdbGet failed");
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "kdbSet failed");
ksDel (ks);
ks = ksNew (20, KS_END);
ksAppendKey (ks, k2);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "kdbGet should have failed");
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "kdbSet should have failed");
ksDel (ks);
ks = ksNew (20, KS_END);
ksAppendKey (ks, k3);
succeed_if (plugin->kdbGet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "kdbGet failed");
succeed_if (plugin->kdbSet (plugin, ks, parentKey) == ELEKTRA_PLUGIN_STATUS_SUCCESS, "kdbSet failed");
ksDel (ks);
keyDel (parentKey);
PLUGIN_CLOSE ();
}
int main (int argc, char ** argv)
{
printf ("BLACKLIST TESTS\n");
printf ("==================\n\n");
init (argc, argv);
test_blacklist ();
test_blacklist_empty_values ();
test_blacklist_null_values ();
print_result ("testmod_blacklist");
return nbError;
}