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 <tests_internal.h>
10 :
11 : #include "kdbtools.h"
12 : #include "xmltool.h"
13 :
14 : #include <unistd.h>
15 :
16 0 : void test_readwrite (void)
17 : {
18 : KeySet * ks;
19 : FILE * fout;
20 :
21 0 : printf ("Testing Read and write xml\n");
22 :
23 0 : fout = fopen (srcdir_file ("xmltool/key-gen.xml"), "w");
24 0 : ks = ksNew (0, KS_END);
25 0 : exit_if_fail (ksFromXMLfile (ks, srcdir_file ("xmltool/key.xml")) == 0, "ksFromXMLfile(key.xml) failed.");
26 0 : ksToStream (ks, fout, KDB_O_HEADER);
27 0 : fclose (fout);
28 :
29 0 : compare_files (srcdir_file ("xmltool/key.xml"));
30 0 : unlink (srcdir_file ("xmltool/key-gen.xml"));
31 :
32 0 : ksDel (ks);
33 0 : }
34 :
35 0 : void test_readwrite_hier (void)
36 : {
37 : KeySet * ks;
38 : FILE * fout;
39 :
40 0 : printf ("Testing Read and write xml\n");
41 :
42 0 : fout = fopen (srcdir_file ("xmltool/key-gen.xml"), "w");
43 0 : ks = ksNew (0, KS_END);
44 0 : exit_if_fail (ksFromXMLfile (ks, srcdir_file ("xmltool/key.xml")) == 0, "ksFromXMLfile(key.xml) failed.");
45 0 : ksToStream (ks, fout, KDB_O_HIER | KDB_O_HEADER);
46 0 : fclose (fout);
47 :
48 0 : compare_files (srcdir_file ("xmltool/key.xml"));
49 0 : unlink (srcdir_file ("xmltool/key-gen.xml"));
50 :
51 0 : ksDel (ks);
52 0 : }
53 :
54 :
55 1 : void test_key (void)
56 : {
57 : KeySet * ks;
58 : Key * cur;
59 : int counter;
60 :
61 1 : printf ("Testing Key from xml\n");
62 :
63 1 : ks = ksNew (0, KS_END);
64 1 : exit_if_fail (ksFromXMLfile (ks, srcdir_file ("xmltool/key.xml")) == 0, "ksFromXMLfile(key.xml) failed.");
65 1 : counter = 0;
66 1 : ksRewind (ks);
67 11 : while ((cur = ksNext (ks)))
68 : {
69 9 : counter++;
70 : /* Prepend key root */
71 : // snprintf(buf, sizeof(buf), "%s/%s", root, keyName(cur));
72 : // keySetName(cur, buf);
73 :
74 : /* Make tests ... */
75 : // printf ("counter: %d - name: %s - value: %s\n", counter, (char*) keyName(cur), (char*)keyValue(cur));
76 9 : switch (counter)
77 : {
78 : case 1:
79 1 : succeed_if (strcmp (keyName (cur), "user/tests/filesys/.HiddenBinaryKey") == 0, "wrong name");
80 1 : succeed_if (strcmp (keyValue (cur), "BinaryValue") == 0, "value not correct");
81 1 : succeed_if (strcmp (keyComment (cur), "Binary key with hidden name") == 0, "comment not correct");
82 1 : succeed_if (keyIsInactive (cur) == 1, "key is inactive");
83 1 : succeed_if (keyIsBinary (cur) == 1, "key is not binary");
84 1 : succeed_if (keyGetMode (cur) == 0440, "could not get mode");
85 : break;
86 : case 2:
87 1 : succeed_if (strcmp (keyName (cur), "user/tests/filesys/.HiddenDirectoryKey") == 0, "wrong name");
88 1 : succeed_if (strcmp (keyValue (cur), "DirectoryValue") == 0, "value not correct");
89 1 : succeed_if (strcmp (keyComment (cur), "Directory key with hidden name") == 0, "comment not correct");
90 1 : succeed_if (keyIsInactive (cur) == 1, "key is inactive");
91 1 : succeed_if (keyIsString (cur) == 1, "key is not string");
92 : break;
93 : case 3:
94 1 : succeed_if (strcmp (keyName (cur), "user/tests/filesys/.HiddenStringKey") == 0, "wrong name");
95 1 : succeed_if (strcmp (keyValue (cur), "StringValue") == 0, "value not correct");
96 1 : succeed_if (strcmp (keyComment (cur), "String key with hidden name") == 0, "comment not correct");
97 1 : succeed_if (keyIsInactive (cur) == 1, "key is inactive");
98 1 : succeed_if (keyIsString (cur) == 1, "key is not string");
99 1 : succeed_if (keyGetUID (cur) == 0, "could not get uid value");
100 1 : succeed_if (keyGetGID (cur) == 20, "could not get gid value");
101 : break;
102 : case 4:
103 1 : succeed_if (strcmp (keyName (cur), "user/tests/filesys/PerfectBinaryKey") == 0, "wrong name");
104 1 : succeed_if (strcmp (keyValue (cur), "BinaryValue") == 0, "value not correct");
105 1 : succeed_if (strcmp (keyComment (cur), "Binary key with standard name") == 0, "comment not correct");
106 1 : succeed_if (keyIsInactive (cur) == 0, "key is active");
107 1 : succeed_if (keyIsBinary (cur) == 1, "key is not binary");
108 1 : succeed_if (keyGetGID (cur) == 40, "could not get gid value");
109 : break;
110 : case 5:
111 1 : succeed_if (strcmp (keyName (cur), "user/tests/filesys/PerfectDirectoryKey") == 0, "wrong name");
112 1 : succeed_if (strcmp (keyValue (cur), "DirectoryValue") == 0, "value not correct");
113 1 : succeed_if (strcmp (keyComment (cur), "Directory key with standard name") == 0, "comment not correct");
114 1 : succeed_if (keyIsInactive (cur) == 0, "key is active");
115 1 : succeed_if (keyIsString (cur) == 1, "key is not string");
116 : break;
117 : case 6:
118 1 : succeed_if (strcmp (keyName (cur), "user/tests/filesys/PerfectStringKey") == 0, "wrong name");
119 1 : succeed_if (strcmp (keyValue (cur), "StringValue") == 0, "value not correct");
120 1 : succeed_if (strcmp (keyComment (cur), "String key with\nstandard name") == 0, "comment not correct");
121 1 : succeed_if (keyIsInactive (cur) == 0, "key is active");
122 1 : succeed_if (keyIsString (cur) == 1, "key is not string");
123 1 : succeed_if (keyGetUID (cur) == 20, "could not get uid value");
124 : break;
125 : case 7:
126 1 : succeed_if (strcmp (keyName (cur), "user/tests/filesys/Ug.ly:Bin@a€ry Key") == 0, "wrong name");
127 1 : succeed_if (keyValue (cur) == 0, "value not correct");
128 1 : succeed_if (strcmp (keyComment (cur), "Binary key with ugly name") == 0, "comment not correct");
129 1 : succeed_if (keyIsInactive (cur) == 0, "key is active");
130 1 : succeed_if (keyIsBinary (cur) == 1, "key is not binary");
131 1 : succeed_if (keyGetMode (cur) == 230, "could not get mode");
132 : // <key type="binary" basename="Ug.ly:Bin@a€ry Key"><comment>Binary key with ugly name</comment></key>
133 : break;
134 : case 8:
135 1 : succeed_if (strcmp (keyName (cur), "user/tests/filesys/Ug.ly:Dir@ect€ory Key") == 0, "wrong name");
136 1 : succeed_if (strcmp (keyValue (cur), "") == 0, "value not correct");
137 1 : succeed_if (strcmp (keyComment (cur), "Directory with ugly name") == 0, "comment not correct");
138 1 : succeed_if (keyIsInactive (cur) == 0, "key is active");
139 1 : succeed_if (keyIsString (cur) == 1, "key is not string");
140 : // <key type="directory" basename="Ug.ly:Dir@ect€ory Key"><comment>Directory with ugly name</comment></key>
141 : break;
142 : case 9:
143 1 : succeed_if (strcmp (keyName (cur), "user/tests/filesys/Ug.ly:St@ri€n.g Key") == 0, "wrong name");
144 1 : succeed_if (strcmp (keyValue (cur), "With a string value") == 0, "value not correct");
145 1 : succeed_if (strcmp (keyComment (cur), "string key with ugly name") == 0, "comment not correct");
146 1 : succeed_if (keyIsInactive (cur) == 0, "key is active");
147 1 : succeed_if (keyIsString (cur) == 1, "key is not string");
148 : // <key type="string" basename="Ug.ly:St@ri€n.g Key" value="With a string value"><comment>string key with ugly
149 : // name</comment></key>
150 : break;
151 : }
152 : }
153 :
154 1 : ksDel (ks);
155 1 : }
156 :
157 1 : void test_keyset (void)
158 : {
159 : KeySet * ks;
160 : Key * cur;
161 : int counter;
162 :
163 1 : printf ("Testing KeySet from xml\n");
164 :
165 1 : ks = ksNew (0, KS_END);
166 1 : exit_if_fail (ksFromXMLfile (ks, srcdir_file ("xmltool/keyset.xml")) == 0, "ksFromXMLfile(key.xml) failed.");
167 1 : counter = 0;
168 1 : ksRewind (ks);
169 94 : while ((cur = ksNext (ks)))
170 : {
171 92 : counter++;
172 :
173 : /* Make tests ... */
174 : // printf ("counter: %d - %s\n", counter, keyName(cur));
175 92 : switch (counter)
176 : {
177 : // <key type="43" basename="0-27042916" value="0 216905227"><comment>2551516588474823843</comment></key>
178 : case 1:
179 1 : succeed_if (strcmp (keyName (cur), "user/tests/filesys/0-27042916") == 0, "name of first key not correct");
180 1 : succeed_if (strcmp (keyValue (cur), "0 216905227") == 0, "value of first key not correct");
181 1 : succeed_if (strcmp (keyComment (cur), "2551516588474823843") == 0, "comment of first key not correct");
182 : break;
183 : // <key type="253" basename="1-2449524622" value="1 1679328197"><comment>3246436893195629244</comment></key>
184 : case 2:
185 1 : succeed_if (strcmp (keyName (cur), "user/tests/filesys/1-2449524622") == 0, "name of 2. key not correct");
186 1 : succeed_if (strcmp (keyValue (cur), "1 1679328197") == 0, "value of 2. key not correct");
187 1 : succeed_if (strcmp (keyComment (cur), "3246436893195629244") == 0, "comment of 2. key not correct");
188 : break;
189 : // <key type="string" basename="dir-1-0">
190 : case 3:
191 1 : succeed_if (strcmp (keyName (cur), "user/tests/filesys/dir-1-0") == 0, "name of 3. key not correct");
192 : break;
193 : // <key type="114" basename="0-294164813" value="0 216245011"><comment>18454108762891828026</comment></key>
194 : case 4:
195 1 : succeed_if (strcmp (keyName (cur), "user/tests/filesys/dir-1-0/0-294164813") == 0, "name of 4. key not correct");
196 1 : succeed_if (strcmp (keyValue (cur), "0 216245011") == 0, "value of 4. key not correct");
197 1 : succeed_if (strcmp (keyComment (cur), "18454108762891828026") == 0, "comment of 4. key not correct");
198 : break;
199 : // <key type="135" basename="1-1479930365" value="1 2732423037"><comment>24597295372375238</comment></key>
200 : case 5:
201 1 : succeed_if (strcmp (keyName (cur), "user/tests/filesys/dir-1-0/1-1479930365") == 0, "name of 4. key not correct");
202 1 : succeed_if (strcmp (keyValue (cur), "1 2732423037") == 0, "value of 4. key not correct");
203 1 : succeed_if (strcmp (keyComment (cur), "24597295372375238") == 0, "comment of 4. key not correct");
204 : break;
205 : // <key type="string" basename="dir-2-0">
206 : case 6:
207 1 : succeed_if (strcmp (keyName (cur), "user/tests/filesys/dir-1-0/dir-2-0") == 0, "name of 3. key not correct");
208 : break;
209 : // <key type="144" basename="0-215571059" value="0 264857705"><comment>2188631490667217086</comment></key>
210 : case 7:
211 1 : succeed_if (strcmp (keyName (cur), "user/tests/filesys/dir-1-0/dir-2-0/0-215571059") == 0,
212 : "name of 4. key not correct");
213 1 : succeed_if (strcmp (keyValue (cur), "0 264857705") == 0, "value of 4. key not correct");
214 1 : succeed_if (strcmp (keyComment (cur), "2188631490667217086") == 0, "comment of 4. key not correct");
215 : break;
216 : }
217 : }
218 :
219 1 : ksDel (ks);
220 1 : }
221 :
222 : #define MAX_SIZE 200
223 :
224 1 : static void test_ksCommonParentName (void)
225 : {
226 : char ret[MAX_SIZE + 1];
227 1 : KeySet * ks =
228 1 : ksNew (10, keyNew ("system/sw/xorg/Monitors/Monitor1/vrefresh", 0), keyNew ("system/sw/xorg/Monitors/Monitor1/hrefresh", 0),
229 : keyNew ("system/sw/xorg/Monitors/Monitor2/vrefresh", 0), keyNew ("system/sw/xorg/Monitors/Monitor2/hrefresh", 0),
230 : keyNew ("system/sw/xorg/Devices/Device1/driver", 0), keyNew ("system/sw/xorg/Devices/Device1/mode", 0), KS_END);
231 :
232 1 : printf ("Test common parentname\n");
233 :
234 1 : succeed_if (ksGetCommonParentName (ks, ret, MAX_SIZE) > 0, "could not find correct parentname");
235 1 : succeed_if_same_string (ret, "system/sw/xorg");
236 1 : ksDel (ks);
237 :
238 1 : ks = ksNew (10, keyNew ("system", 0), keyNew ("user", 0), KS_END);
239 1 : succeed_if (ksGetCommonParentName (ks, ret, MAX_SIZE) == 0, "could find correct parentname");
240 1 : succeed_if_same_string (ret, "");
241 1 : ksDel (ks);
242 :
243 1 : ks = ksNew (10, keyNew ("system/some/thing", 0), keyNew ("system/other/thing", 0), KS_END);
244 1 : succeed_if (ksGetCommonParentName (ks, ret, MAX_SIZE) == 7, "could find correct parentname");
245 1 : succeed_if_same_string (ret, "system");
246 1 : ksDel (ks);
247 :
248 1 : ks = ksNew (10, keyNew ("system/here/in/deep/goes/ok/thing", 0), keyNew ("system/here/in/deep/goes/ok/other/thing", 0), KS_END);
249 1 : succeed_if (ksGetCommonParentName (ks, ret, MAX_SIZE) > 0, "could find correct parentname");
250 1 : succeed_if_same_string (ret, "system/here/in/deep/goes/ok");
251 1 : ksDel (ks);
252 :
253 1 : ks = ksNew (10, keyNew ("system/here/in/deep/goes/ok/thing", 0), keyNew ("system/here/in/deep/goes/ok/other/thing", 0),
254 : keyNew ("user/unique/thing", 0), KS_END);
255 1 : succeed_if (ksGetCommonParentName (ks, ret, MAX_SIZE) == 0, "could find correct parentname");
256 1 : succeed_if_same_string (ret, "");
257 1 : ksDel (ks);
258 :
259 1 : ks = ksNew (10, keyNew ("user/unique/thing", 0), KS_END);
260 1 : succeed_if (ksGetCommonParentName (ks, ret, MAX_SIZE) > 0, "could find correct parentname");
261 1 : succeed_if_same_string (ret, "user/unique/thing");
262 1 : ksDel (ks);
263 1 : }
264 :
265 :
266 1 : int main (int argc, char ** argv)
267 : {
268 1 : printf ("ELEKTRA PLUGIN TEST SUITE\n");
269 1 : printf ("========================================\n\n");
270 :
271 1 : init (argc, argv);
272 :
273 1 : test_key ();
274 1 : test_keyset ();
275 1 : test_ksCommonParentName ();
276 :
277 : /*
278 : test_readwrite();
279 : test_readwrite_hier();
280 : */
281 :
282 1 : print_result ("test_xml");
283 :
284 1 : return nbError;
285 : }
|