virtio-9p: Implement Security model for mksock using mknod.
[qemu/mdroth.git] / check-qdict.c
blob2c3089fa66d1b8710c188c0729139a67cc2685ca
1 /*
2 * QDict unit-tests.
4 * Copyright (C) 2009 Red Hat Inc.
6 * Authors:
7 * Luiz Capitulino <lcapitulino@redhat.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
12 #include <check.h>
14 #include "qint.h"
15 #include "qdict.h"
16 #include "qstring.h"
17 #include "qemu-common.h"
20 * Public Interface test-cases
22 * (with some violations to access 'private' data)
25 START_TEST(qdict_new_test)
27 QDict *qdict;
29 qdict = qdict_new();
30 fail_unless(qdict != NULL);
31 fail_unless(qdict_size(qdict) == 0);
32 fail_unless(qdict->base.refcnt == 1);
33 fail_unless(qobject_type(QOBJECT(qdict)) == QTYPE_QDICT);
35 // destroy doesn't exit yet
36 free(qdict);
38 END_TEST
40 START_TEST(qdict_put_obj_test)
42 QInt *qi;
43 QDict *qdict;
44 QDictEntry *ent;
45 const int num = 42;
47 qdict = qdict_new();
49 // key "" will have tdb hash 12345
50 qdict_put_obj(qdict, "", QOBJECT(qint_from_int(num)));
52 fail_unless(qdict_size(qdict) == 1);
53 ent = QLIST_FIRST(&qdict->table[12345 % QDICT_HASH_SIZE]);
54 qi = qobject_to_qint(ent->value);
55 fail_unless(qint_get_int(qi) == num);
57 // destroy doesn't exit yet
58 QDECREF(qi);
59 qemu_free(ent->key);
60 qemu_free(ent);
61 qemu_free(qdict);
63 END_TEST
65 START_TEST(qdict_destroy_simple_test)
67 QDict *qdict;
69 qdict = qdict_new();
70 qdict_put_obj(qdict, "num", QOBJECT(qint_from_int(0)));
71 qdict_put_obj(qdict, "str", QOBJECT(qstring_from_str("foo")));
73 QDECREF(qdict);
75 END_TEST
77 static QDict *tests_dict = NULL;
79 static void qdict_setup(void)
81 tests_dict = qdict_new();
82 fail_unless(tests_dict != NULL);
85 static void qdict_teardown(void)
87 QDECREF(tests_dict);
88 tests_dict = NULL;
91 START_TEST(qdict_get_test)
93 QInt *qi;
94 QObject *obj;
95 const int value = -42;
96 const char *key = "test";
98 qdict_put(tests_dict, key, qint_from_int(value));
100 obj = qdict_get(tests_dict, key);
101 fail_unless(obj != NULL);
103 qi = qobject_to_qint(obj);
104 fail_unless(qint_get_int(qi) == value);
106 END_TEST
108 START_TEST(qdict_get_int_test)
110 int ret;
111 const int value = 100;
112 const char *key = "int";
114 qdict_put(tests_dict, key, qint_from_int(value));
116 ret = qdict_get_int(tests_dict, key);
117 fail_unless(ret == value);
119 END_TEST
121 START_TEST(qdict_get_try_int_test)
123 int ret;
124 const int value = 100;
125 const char *key = "int";
127 qdict_put(tests_dict, key, qint_from_int(value));
129 ret = qdict_get_try_int(tests_dict, key, 0);
130 fail_unless(ret == value);
132 END_TEST
134 START_TEST(qdict_get_str_test)
136 const char *p;
137 const char *key = "key";
138 const char *str = "string";
140 qdict_put(tests_dict, key, qstring_from_str(str));
142 p = qdict_get_str(tests_dict, key);
143 fail_unless(p != NULL);
144 fail_unless(strcmp(p, str) == 0);
146 END_TEST
148 START_TEST(qdict_get_try_str_test)
150 const char *p;
151 const char *key = "key";
152 const char *str = "string";
154 qdict_put(tests_dict, key, qstring_from_str(str));
156 p = qdict_get_try_str(tests_dict, key);
157 fail_unless(p != NULL);
158 fail_unless(strcmp(p, str) == 0);
160 END_TEST
162 START_TEST(qdict_haskey_not_test)
164 fail_unless(qdict_haskey(tests_dict, "test") == 0);
166 END_TEST
168 START_TEST(qdict_haskey_test)
170 const char *key = "test";
172 qdict_put(tests_dict, key, qint_from_int(0));
173 fail_unless(qdict_haskey(tests_dict, key) == 1);
175 END_TEST
177 START_TEST(qdict_del_test)
179 const char *key = "key test";
181 qdict_put(tests_dict, key, qstring_from_str("foo"));
182 fail_unless(qdict_size(tests_dict) == 1);
184 qdict_del(tests_dict, key);
186 fail_unless(qdict_size(tests_dict) == 0);
187 fail_unless(qdict_haskey(tests_dict, key) == 0);
189 END_TEST
191 START_TEST(qobject_to_qdict_test)
193 fail_unless(qobject_to_qdict(QOBJECT(tests_dict)) == tests_dict);
195 END_TEST
198 * Errors test-cases
201 START_TEST(qdict_put_exists_test)
203 int value;
204 const char *key = "exists";
206 qdict_put(tests_dict, key, qint_from_int(1));
207 qdict_put(tests_dict, key, qint_from_int(2));
209 value = qdict_get_int(tests_dict, key);
210 fail_unless(value == 2);
212 fail_unless(qdict_size(tests_dict) == 1);
214 END_TEST
216 START_TEST(qdict_get_not_exists_test)
218 fail_unless(qdict_get(tests_dict, "foo") == NULL);
220 END_TEST
223 * Stress test-case
225 * This is a lot big for a unit-test, but there is no other place
226 * to have it.
229 static void remove_dots(char *string)
231 char *p = strchr(string, ':');
232 if (p)
233 *p = '\0';
236 static QString *read_line(FILE *file, char *key)
238 char value[128];
240 if (fscanf(file, "%s%s", key, value) == EOF)
241 return NULL;
242 remove_dots(key);
243 return qstring_from_str(value);
246 #define reset_file(file) fseek(file, 0L, SEEK_SET)
248 START_TEST(qdict_stress_test)
250 size_t lines;
251 char key[128];
252 FILE *test_file;
253 QDict *qdict;
254 QString *value;
255 const char *test_file_path = "qdict-test-data.txt";
257 test_file = fopen(test_file_path, "r");
258 fail_unless(test_file != NULL);
260 // Create the dict
261 qdict = qdict_new();
262 fail_unless(qdict != NULL);
264 // Add everything from the test file
265 for (lines = 0;; lines++) {
266 value = read_line(test_file, key);
267 if (!value)
268 break;
270 qdict_put(qdict, key, value);
272 fail_unless(qdict_size(qdict) == lines);
274 // Check if everything is really in there
275 reset_file(test_file);
276 for (;;) {
277 const char *str1, *str2;
279 value = read_line(test_file, key);
280 if (!value)
281 break;
283 str1 = qstring_get_str(value);
285 str2 = qdict_get_str(qdict, key);
286 fail_unless(str2 != NULL);
288 fail_unless(strcmp(str1, str2) == 0);
290 QDECREF(value);
293 // Delete everything
294 reset_file(test_file);
295 for (;;) {
296 value = read_line(test_file, key);
297 if (!value)
298 break;
300 qdict_del(qdict, key);
301 QDECREF(value);
303 fail_unless(qdict_haskey(qdict, key) == 0);
305 fclose(test_file);
307 fail_unless(qdict_size(qdict) == 0);
308 QDECREF(qdict);
310 END_TEST
312 static Suite *qdict_suite(void)
314 Suite *s;
315 TCase *qdict_public_tcase;
316 TCase *qdict_public2_tcase;
317 TCase *qdict_stress_tcase;
318 TCase *qdict_errors_tcase;
320 s = suite_create("QDict test-suite");
322 qdict_public_tcase = tcase_create("Public Interface");
323 suite_add_tcase(s, qdict_public_tcase);
324 tcase_add_test(qdict_public_tcase, qdict_new_test);
325 tcase_add_test(qdict_public_tcase, qdict_put_obj_test);
326 tcase_add_test(qdict_public_tcase, qdict_destroy_simple_test);
328 /* Continue, but now with fixtures */
329 qdict_public2_tcase = tcase_create("Public Interface (2)");
330 suite_add_tcase(s, qdict_public2_tcase);
331 tcase_add_checked_fixture(qdict_public2_tcase, qdict_setup, qdict_teardown);
332 tcase_add_test(qdict_public2_tcase, qdict_get_test);
333 tcase_add_test(qdict_public2_tcase, qdict_get_int_test);
334 tcase_add_test(qdict_public2_tcase, qdict_get_try_int_test);
335 tcase_add_test(qdict_public2_tcase, qdict_get_str_test);
336 tcase_add_test(qdict_public2_tcase, qdict_get_try_str_test);
337 tcase_add_test(qdict_public2_tcase, qdict_haskey_not_test);
338 tcase_add_test(qdict_public2_tcase, qdict_haskey_test);
339 tcase_add_test(qdict_public2_tcase, qdict_del_test);
340 tcase_add_test(qdict_public2_tcase, qobject_to_qdict_test);
342 qdict_errors_tcase = tcase_create("Errors");
343 suite_add_tcase(s, qdict_errors_tcase);
344 tcase_add_checked_fixture(qdict_errors_tcase, qdict_setup, qdict_teardown);
345 tcase_add_test(qdict_errors_tcase, qdict_put_exists_test);
346 tcase_add_test(qdict_errors_tcase, qdict_get_not_exists_test);
348 /* The Big one */
349 qdict_stress_tcase = tcase_create("Stress Test");
350 suite_add_tcase(s, qdict_stress_tcase);
351 tcase_add_test(qdict_stress_tcase, qdict_stress_test);
353 return s;
356 int main(void)
358 int nf;
359 Suite *s;
360 SRunner *sr;
362 s = qdict_suite();
363 sr = srunner_create(s);
365 srunner_run_all(sr, CK_NORMAL);
366 nf = srunner_ntests_failed(sr);
367 srunner_free(sr);
369 return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;