8 #if !defined(ck_assert_ptr_null)
9 # define ck_assert_ptr_null(X) ck_assert_ptr_eq(X, NULL)
10 # define ck_assert_ptr_nonnull(X) ck_assert_ptr_ne(X, NULL)
14 #include "afb-apiset.h"
16 const char *names[] = {
32 const char *aliases[] = {
44 const char *extras[] = {
58 struct afb_api_itf api_itf_null = {
60 .service_start = NULL,
69 /*********************************************************************/
70 /* check the initialisation */
71 START_TEST (check_initialisation)
73 const char name[] = "name";
74 const char noname[] = "";
77 struct afb_apiset *a, *b;
79 a = afb_apiset_create(NULL, noto);
80 ck_assert_ptr_nonnull(a);
81 ck_assert_str_eq(noname, afb_apiset_name(a));
82 ck_assert_int_eq(noto, afb_apiset_timeout_get(a));
83 afb_apiset_timeout_set(a, to);
84 ck_assert_int_eq(to, afb_apiset_timeout_get(a));
85 b = afb_apiset_addref(a);
86 ck_assert_ptr_eq(a, b);
90 a = afb_apiset_create(name, to);
91 ck_assert_ptr_nonnull(a);
92 ck_assert_str_eq(name, afb_apiset_name(a));
93 ck_assert_int_eq(to, afb_apiset_timeout_get(a));
94 afb_apiset_timeout_set(a, noto);
95 ck_assert_int_eq(noto, afb_apiset_timeout_get(a));
96 b = afb_apiset_addref(a);
97 ck_assert_ptr_eq(a, b);
103 /*********************************************************************/
104 /* check that NULL is a valid value for addref/unref */
105 START_TEST (check_sanity)
107 struct afb_apiset *a;
109 a = afb_apiset_addref(NULL);
110 ck_assert_ptr_null(a);
111 afb_apiset_unref(NULL);
116 /*********************************************************************/
117 /* check creation and retrieval of apis */
119 START_TEST (check_creation)
122 struct afb_apiset *a;
123 struct afb_api_item sa;
124 const char *x, *y, **set;
125 const struct afb_api_item *pa;
127 /* create a apiset */
128 a = afb_apiset_create(NULL, 0);
129 ck_assert_ptr_nonnull(a);
132 for (i = 0 ; names[i] != NULL ; i++) {
133 sa.itf = &api_itf_null;
134 sa.closure = (void*)names[i];
136 ck_assert_int_eq(0, afb_apiset_add(a, names[i], sa));
137 pa = afb_apiset_lookup(a, names[i], 1);
138 ck_assert_ptr_nonnull(pa);
139 ck_assert_ptr_eq(sa.itf, pa->itf);
140 ck_assert_ptr_eq(sa.closure, pa->closure);
141 ck_assert_ptr_eq(sa.group, pa->group);
142 ck_assert_int_eq(0, afb_apiset_is_alias(a, names[i]));
143 ck_assert_str_eq(names[i], afb_apiset_unalias(a, names[i]));
144 ck_assert_int_eq(-1, afb_apiset_add(a, names[i], sa));
145 ck_assert_int_eq(errno, EEXIST);
150 for (i = 0 ; aliases[i] != NULL ; i += 2) {
151 ck_assert_int_eq(-1, afb_apiset_add_alias(a, extras[0], aliases[i]));
152 ck_assert_int_eq(errno, ENOENT);
153 ck_assert_int_eq(0, afb_apiset_add_alias(a, aliases[i + 1], aliases[i]));
154 ck_assert_ptr_nonnull(afb_apiset_lookup(a, aliases[i], 1));
155 ck_assert_int_eq(1, afb_apiset_is_alias(a, aliases[i]));
156 x = afb_apiset_unalias(a, aliases[i]);
157 y = afb_apiset_unalias(a, aliases[i + 1]);
158 ck_assert_int_eq(0, strcasecmp(x, y));
159 ck_assert_int_eq(-1, afb_apiset_add_alias(a, aliases[i + 1], aliases[i]));
160 ck_assert_int_eq(errno, EEXIST);
165 for (i = 0 ; extras[i] != NULL ; i++) {
166 pa = afb_apiset_lookup(a, extras[i], 1);
167 ck_assert_ptr_null(pa);
168 ck_assert_int_eq(errno, ENOENT);
172 set = afb_apiset_get_names(a, 0, 1);
173 ck_assert_ptr_nonnull(set);
174 for (i = 0 ; set[i] != NULL ; i++) {
175 ck_assert_ptr_nonnull(afb_apiset_lookup(a, set[i], 0));
176 ck_assert_int_eq(0, afb_apiset_is_alias(a, set[i]));
178 ck_assert_int_gt(0, strcasecmp(set[i-1], set[i]));
180 ck_assert_int_eq(i, nn);
182 set = afb_apiset_get_names(a, 0, 2);
183 ck_assert_ptr_nonnull(set);
184 for (i = 0 ; set[i] != NULL ; i++) {
185 ck_assert_ptr_nonnull(afb_apiset_lookup(a, set[i], 0));
186 ck_assert_int_eq(1, afb_apiset_is_alias(a, set[i]));
188 ck_assert_int_gt(0, strcasecmp(set[i-1], set[i]));
190 ck_assert_int_eq(i, na);
192 set = afb_apiset_get_names(a, 0, 3);
193 ck_assert_ptr_nonnull(set);
194 for (i = 0 ; set[i] != NULL ; i++) {
195 ck_assert_ptr_nonnull(afb_apiset_lookup(a, set[i], 0));
197 ck_assert_int_gt(0, strcasecmp(set[i-1], set[i]));
199 ck_assert_int_eq(i, nn + na);
201 /* removes the apis to check deletion */
202 for (i = 0 ; i < nn + na ; i++) {
206 /* should be present */
207 ck_assert_ptr_nonnull(afb_apiset_lookup(a, set[i], 0));
209 /* deleting a non aliased api removes the aliases! */
210 if (!afb_apiset_is_alias(a, set[i])) {
211 for (j = i + 1 ; j < nn + na ; j++) {
214 ck_assert_ptr_nonnull(afb_apiset_lookup(a, set[j], 0));
215 if (afb_apiset_is_alias(a, set[j])
216 && afb_apiset_lookup(a, set[i], 0) == afb_apiset_lookup(a, set[j], 0)) {
217 ck_assert(set[j][0] > 0);
218 ((char*)set[j])[0] = (char)-set[j][0];
224 ck_assert_int_eq(0, afb_apiset_del(a, set[i]));
225 ck_assert_ptr_null(afb_apiset_lookup(a, set[i], 0));
227 /* check other not removed except aliases */
228 for (j = i + 1 ; j < nn + na ; j++) {
232 ck_assert_ptr_nonnull(afb_apiset_lookup(a, set[j], 0));
234 ((char*)set[j])[0] = (char)-set[j][0];
235 ck_assert_ptr_null(afb_apiset_lookup(a, set[j], 0));
246 /*********************************************************************/
247 /* check onlack behaviour */
251 static void onlackcleanup(void *closure)
253 int *count = closure;
254 ck_assert_ptr_eq(count, &onlackcount);
257 static int onlack(void *closure, struct afb_apiset *a, const char *name)
259 int *count = closure;
260 struct afb_api_item sa;
262 ck_assert_ptr_eq(count, &onlackcount);
265 sa.itf = &api_itf_null;
266 sa.closure = (void*)name;
269 ck_assert_int_eq(0, afb_apiset_add(a, name, sa));
273 START_TEST (check_onlack)
276 struct afb_apiset *a;
277 struct afb_api_item sa;
279 const struct afb_api_item *pa;
281 /* create a apiset */
282 a = afb_apiset_create(NULL, 0);
283 ck_assert_ptr_nonnull(a);
286 for (i = 0 ; names[i] != NULL ; i++) {
287 sa.itf = &api_itf_null;
288 sa.closure = (void*)names[i];
290 ck_assert_int_eq(0, afb_apiset_add(a, names[i], sa));
291 pa = afb_apiset_lookup(a, names[i], 1);
292 ck_assert_ptr_nonnull(pa);
293 ck_assert_ptr_eq(sa.itf, pa->itf);
294 ck_assert_ptr_eq(sa.closure, pa->closure);
295 ck_assert_ptr_eq(sa.group, pa->group);
296 ck_assert_int_eq(0, afb_apiset_is_alias(a, names[i]));
297 ck_assert_str_eq(names[i], afb_apiset_unalias(a, names[i]));
298 ck_assert_int_eq(-1, afb_apiset_add(a, names[i], sa));
299 ck_assert_int_eq(errno, EEXIST);
303 for (i = 0 ; aliases[i] != NULL ; i += 2) {
304 ck_assert_int_eq(-1, afb_apiset_add_alias(a, extras[0], aliases[i]));
305 ck_assert_int_eq(errno, ENOENT);
306 ck_assert_int_eq(0, afb_apiset_add_alias(a, aliases[i + 1], aliases[i]));
307 ck_assert_ptr_nonnull(afb_apiset_lookup(a, aliases[i], 1));
308 ck_assert_int_eq(1, afb_apiset_is_alias(a, aliases[i]));
309 x = afb_apiset_unalias(a, aliases[i]);
310 y = afb_apiset_unalias(a, aliases[i + 1]);
311 ck_assert_int_eq(0, strcasecmp(x, y));
312 ck_assert_int_eq(-1, afb_apiset_add_alias(a, aliases[i + 1], aliases[i]));
313 ck_assert_int_eq(errno, EEXIST);
317 for (i = 0 ; extras[i] != NULL ; i++) {
318 pa = afb_apiset_lookup(a, extras[i], 1);
319 ck_assert_ptr_null(pa);
320 ck_assert_int_eq(errno, ENOENT);
323 /* put the onlack feature */
324 afb_apiset_onlack_set(a, onlack, &onlackcount, onlackcleanup);
328 for (i = 0 ; extras[i] != NULL ; i++) {
329 ck_assert_int_eq(onlackcount, i);
330 pa = afb_apiset_lookup(a, extras[i], 1);
331 ck_assert_int_eq(onlackcount, i + 1);
332 ck_assert_ptr_nonnull(pa);
333 ck_assert_ptr_eq(&api_itf_null, pa->itf);
334 ck_assert_ptr_eq(extras[i], pa->closure);
335 ck_assert_ptr_eq(extras[i], pa->group);
338 ck_assert_int_eq(onlackcount, i);
340 ck_assert_int_eq(onlackcount, 0);
344 /*********************************************************************/
356 { "Clarence", 0, 0 },
367 struct set_api *set_last_api;
369 void set_cb0(void *closure)
371 set_last_api = closure;
375 void set_cb_setmask(void *closure, int mask)
378 set_last_api->mask = mask;
381 int set_cb_getmask(void *closure)
384 return set_last_api->mask;
387 int set_cb_start(void *closure)
390 ck_assert_int_eq(0, set_last_api->init);
391 set_last_api->init = 1;
395 struct afb_api_itf set_api_itf = {
397 .service_start = set_cb_start,
398 .update_hooks = set_cb0,
399 .get_logmask = set_cb_getmask,
400 .set_logmask = set_cb_setmask,
405 START_TEST (check_settings)
408 struct afb_apiset *a;
409 struct afb_api_item sa;
411 /* create a apiset */
412 a = afb_apiset_create(NULL, 0);
413 ck_assert_ptr_nonnull(a);
416 for (i = 0 ; set_apis[i].name != NULL ; i++) {
417 sa.itf = &set_api_itf;
418 sa.closure = &set_apis[i];
420 ck_assert_int_eq(0, afb_apiset_add(a, set_apis[i].name, sa));
425 afb_apiset_start_all_services(a);
426 ck_assert_int_eq(nn, set_count);
429 afb_apiset_update_hooks(a, NULL);
430 ck_assert_int_eq(nn, set_count);
432 for (mask = 1 ; !(mask >> 10) ; mask <<= 1) {
434 afb_apiset_set_logmask(a, NULL, mask);
435 ck_assert_int_eq(nn, set_count);
437 for (i = 0 ; set_apis[i].name != NULL ; i++) {
438 ck_assert_int_eq(mask, afb_apiset_get_logmask(a, set_apis[i].name));
439 ck_assert_ptr_eq(set_last_api, &set_apis[i]);
440 ck_assert_int_eq(i + 1, set_count);
446 ck_assert_int_eq(nn, set_count);
450 /*********************************************************************/
463 const char *provides;
464 const char *requires;
469 { "Carma", "", "Sadie", "", 0, 9 },
470 { "Cory", "Milford", "", "Clarence", 0, 3 },
471 { "Clarence", "Milford", "", "Jeffery", 0, 2 },
472 { "Jeffery", "Milford", "", "", 0, 1 },
473 { "Molly", "Yvette", "", "Corey", 0, 6 },
474 { "Sheba", "Yvette", "Milford", "Molly", 0, 7 },
475 { "Tasha", "Sadie", "Yvette", "", 0, 8 },
476 { "Corey", "Sadie", "Milford", "Gerry", 0, 5 },
477 { "Gerry", "Sadie", "Milford", "", 0, 4 },
478 { NULL, NULL, NULL, NULL, 0, 0 }
483 int clacb_start(void *closure)
485 struct clapi *a = closure;
488 ck_assert_int_eq(0, a->init);
490 for (i = 0 ; clapi[i].name ; i++) {
491 if (a->requires && a->requires[0]
492 && clapi[i].provides && clapi[i].provides[0]
493 && !strcmp(a->requires, clapi[i].provides))
494 ck_assert_int_ne(0, clapi[i].init);
495 if (a->apireq && a->apireq[0]
496 && !strcmp(a->apireq, clapi[i].name))
497 ck_assert_int_ne(0, clapi[i].init);
500 ck_assert_int_eq(a->init, a->expect);
505 struct afb_api_itf clitf = {
507 .service_start = clacb_start,
508 .update_hooks = NULL,
515 START_TEST (check_classes)
518 struct afb_apiset *a;
519 struct afb_api_item sa;
521 /* create a apiset */
522 a = afb_apiset_create(NULL, 0);
523 ck_assert_ptr_nonnull(a);
526 for (i = 0 ; clapi[i].name != NULL ; i++) {
528 sa.closure = &clapi[i];
530 ck_assert_int_eq(0, afb_apiset_add(a, clapi[i].name, sa));
533 /* add constraints */
534 for (i = 0 ; clapi[i].name != NULL ; i++) {
535 if (clapi[i].provides && clapi[i].provides[0])
536 ck_assert_int_eq(0, afb_apiset_provide_class(a, clapi[i].name, clapi[i].provides));
537 if (clapi[i].requires && clapi[i].requires[0])
538 ck_assert_int_eq(0, afb_apiset_require_class(a, clapi[i].name, clapi[i].requires));
539 if (clapi[i].apireq && clapi[i].apireq[0])
540 ck_assert_int_eq(0, afb_apiset_require(a, clapi[i].name, clapi[i].apireq));
544 ck_assert_int_eq(0, afb_apiset_start_all_services(a));
550 /*********************************************************************/
552 START_TEST (check_subset)
555 struct afb_apiset *a, *b, *c, *d;
557 a = afb_apiset_create_subset_first(NULL, "a", 0);
558 ck_assert_ptr_nonnull(a);
559 ck_assert_str_eq("a", afb_apiset_name(a));
560 ck_assert_ptr_null(afb_apiset_subset_get(a));
562 b = afb_apiset_create_subset_first(a, "b", 0);
563 ck_assert_ptr_nonnull(b);
564 ck_assert_str_eq("b", afb_apiset_name(b));
565 ck_assert_ptr_eq(b, afb_apiset_subset_get(a));
566 ck_assert_ptr_null(afb_apiset_subset_get(b));
568 c = afb_apiset_create_subset_first(a, "c", 0);
569 ck_assert_ptr_nonnull(c);
570 ck_assert_str_eq("c", afb_apiset_name(c));
571 ck_assert_ptr_eq(c, afb_apiset_subset_get(a));
572 ck_assert_ptr_eq(b, afb_apiset_subset_get(c));
573 ck_assert_ptr_null(afb_apiset_subset_get(b));
575 d = afb_apiset_create_subset_last(a, "d", 0);
576 ck_assert_ptr_nonnull(d);
577 ck_assert_str_eq("d", afb_apiset_name(d));
578 ck_assert_ptr_eq(c, afb_apiset_subset_get(a));
579 ck_assert_ptr_eq(b, afb_apiset_subset_get(c));
580 ck_assert_ptr_eq(d, afb_apiset_subset_get(b));
581 ck_assert_ptr_null(afb_apiset_subset_get(d));
583 rc = afb_apiset_subset_set(a, b);
585 ck_assert_ptr_eq(b, afb_apiset_subset_get(a));
586 ck_assert_ptr_eq(d, afb_apiset_subset_get(b));
587 ck_assert_ptr_null(afb_apiset_subset_get(d));
593 /*********************************************************************/
598 void mksuite(const char *name) { suite = suite_create(name); }
599 void addtcase(const char *name) { tcase = tcase_create(name); suite_add_tcase(suite, tcase); }
600 void addtest(TFun fun) { tcase_add_test(tcase, fun); }
604 SRunner *srunner = srunner_create(suite);
605 srunner_run_all(srunner, CK_NORMAL);
606 nerr = srunner_ntests_failed(srunner);
607 srunner_free(srunner);
611 int main(int ac, char **av)
615 addtest(check_initialisation);
616 addtest(check_sanity);
617 addtest(check_creation);
618 addtest(check_onlack);
619 addtest(check_settings);
620 addtest(check_classes);
621 addtest(check_subset);