Add accidentally omitted 'port' parameter to error messages
[trust_router.git] / common / tests / commtest.c
1 #include <talloc.h>
2 #include <assert.h>
3 #include <jansson.h>
4
5 #include <tr_apc.h>
6 #include <tr_comm.h>
7 #include <tr_rp.h>
8 #include <tr_name_internal.h>
9
10 /**********************************************************************/
11 /* APC test stuff */
12 struct apc_entry {
13   const char *id; /* only allows a single entry for now */
14 };
15
16 static TR_APC *apc_entry_to_apc(TALLOC_CTX *mem_ctx, struct apc_entry *ae)
17 {
18   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
19   TR_APC *apcs=NULL;
20
21   apcs=tr_apc_new(tmp_ctx);
22   if (apcs!=NULL) {
23     tr_apc_set_id(apcs, tr_new_name(ae->id));
24     talloc_steal(mem_ctx, apcs);
25   }
26
27   talloc_free(tmp_ctx); 
28   return apcs;
29 }
30
31 /**********************************************************************/
32 /* TR_COMM test stuff */
33
34 struct comm_entry {
35   const char *id;
36   TR_COMM_TYPE type;
37   struct apc_entry *apcs;
38 };
39
40 static TR_COMM *comm_entry_to_comm(TALLOC_CTX *mem_ctx, struct comm_entry *ce)
41 {
42   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
43   TR_COMM *comm=tr_comm_new(tmp_ctx);
44
45   if (comm!=NULL) {
46     tr_comm_set_id(comm, tr_new_name(ce->id));
47     tr_comm_set_type(comm, ce->type);
48     if (ce->apcs!=NULL)
49       tr_comm_set_apcs(comm, apc_entry_to_apc(tmp_ctx, ce->apcs));
50
51     if ((tr_comm_get_id(comm)==NULL) ||
52         ((ce->apcs!=NULL)&&(tr_comm_get_apcs(comm)==NULL)))
53       comm=NULL; /* it's in tmp_ctx, so will be freed */
54     else
55       talloc_steal(mem_ctx, comm);
56   }
57
58   talloc_free(tmp_ctx); 
59   return comm;
60 }
61
62 static int add_comm_set(TR_COMM_TABLE *ctab, struct comm_entry *entries)
63 {
64   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
65   struct comm_entry *this=NULL;
66   size_t ii=0;
67   TR_COMM *new=NULL;
68   int rc=-1;
69
70   for (this=entries,ii=0; this->id!=NULL; this++, ii++) {
71     new=comm_entry_to_comm(tmp_ctx, this);
72     if (new==NULL) {
73       printf("Error creating community %u.\n", (unsigned int)ii+1);
74       rc=1;
75       goto cleanup;
76     }
77     tr_comm_table_add_comm(ctab, new);
78   }
79   /* success */
80   rc=0;
81
82 cleanup:
83   talloc_free(tmp_ctx);
84   return rc;
85 }
86
87 static int verify_comm_set(TR_COMM_TABLE *ctab, struct comm_entry *entries)
88 {
89   struct comm_entry *this=NULL;
90   TR_COMM *comm=NULL;
91   TR_NAME *this_id=NULL;
92
93   for (this=entries; this->id!=NULL; this++) {
94     this_id=tr_new_name(this->id);
95     comm=tr_comm_table_find_comm(ctab, this_id);
96     tr_free_name(this_id); this_id=NULL;
97     
98     if (comm==NULL) {
99       printf("Error, community %s missing from community table.\n", this->id);
100       return -1;
101     }
102     if (tr_comm_get_type(comm)!=this->type) {
103       printf("Error, community %s has wrong type (was %s, expected %s).\n",
104              this->id,
105              tr_comm_type_to_str(tr_comm_get_type(comm)),
106              tr_comm_type_to_str(this->type));
107       return -1;
108     }
109     /* TODO: verify apcs */
110   }
111   return 0;
112 }
113
114 /* removes entry n from ctab */
115 static int remove_comm_set_member(TR_COMM_TABLE *ctab, struct comm_entry *entries, size_t n)
116 {
117   TR_NAME *comm_name=tr_new_name(entries[n].id);
118   TR_COMM *comm=tr_comm_table_find_comm(ctab, comm_name);
119   TR_COMM *comm2=NULL;
120
121   if (comm==NULL) {
122     printf("Can't remove community %s, not in table.\n", entries[n].id);
123     tr_free_name(comm_name);
124     return 1;
125   }
126   
127   tr_comm_table_remove_comm(ctab, comm);
128   comm2=tr_comm_table_find_comm(ctab, comm_name);
129   if (comm2!=NULL) {
130     printf("Community %s still in table after removal.\n", entries[n].id);
131     tr_comm_free(comm);
132     tr_free_name(comm_name);
133     return 2;
134   }
135
136   tr_comm_free(comm);
137   tr_free_name(comm_name);
138   return 0;
139 }
140
141 /**********************************************************************/
142 /* TR_RP_REALM test stuff */
143
144 struct rp_realm_entry {
145   const char *id;
146 };
147
148 static TR_RP_REALM *rp_realm_entry_to_rp_realm(TALLOC_CTX *mem_ctx, struct rp_realm_entry *re)
149 {
150   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
151   TR_RP_REALM *realm=NULL;
152
153   realm=tr_rp_realm_new(tmp_ctx);
154   if (realm!=NULL) {
155     tr_rp_realm_set_id(realm, tr_new_name(re->id));
156     talloc_steal(mem_ctx, realm);
157   }
158
159   talloc_free(tmp_ctx); 
160   return realm;
161 }
162
163 static int add_rp_realm_set(TR_COMM_TABLE *ctab, struct rp_realm_entry *entries)
164 {
165   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
166   struct rp_realm_entry *this=NULL;
167   TR_RP_REALM *realm=NULL;
168   int rc=-1;
169
170   for (this=entries; this->id!=NULL; this++) {
171     realm=rp_realm_entry_to_rp_realm(tmp_ctx, this);
172     if (realm==NULL) {
173       printf("Error creating RP realm %s.\n", this->id);
174       rc=1;
175       goto cleanup;
176     }
177     tr_comm_table_add_rp_realm(ctab, realm);
178   }
179   rc=0;
180
181 cleanup:
182   talloc_free(tmp_ctx);
183   return rc;
184 }
185
186 static int verify_rp_realm_set(TR_COMM_TABLE *ctab, struct rp_realm_entry *entries)
187 {
188   struct rp_realm_entry *this=NULL;
189   TR_RP_REALM *rp_realm=NULL;
190   TR_NAME *this_id=NULL;
191
192   for (this=entries; this->id!=NULL; this++) {
193     this_id=tr_new_name(this->id);
194     rp_realm=tr_comm_table_find_rp_realm(ctab, this_id);
195     tr_free_name(this_id); this_id=NULL;
196     
197     if (rp_realm==NULL) {
198       printf("Error, RP realm %s missing from community table.\n", this->id);
199       return -1;
200     }
201   }
202   return 0;
203 }
204
205 /* removes entry n from ctab */
206 static int remove_rp_realm_set_member(TR_COMM_TABLE *ctab, struct rp_realm_entry *entries, size_t n)
207 {
208   TR_NAME *rp_realm_name=tr_new_name(entries[n].id);
209   TR_RP_REALM *rp_realm=tr_comm_table_find_rp_realm(ctab, rp_realm_name);
210   TR_RP_REALM *rp_realm2=NULL;
211
212   if (rp_realm==NULL) {
213     printf("Can't remove RP realm %s, not in table.\n", entries[n].id);
214     tr_free_name(rp_realm_name);
215     return 1;
216   }
217   
218   tr_comm_table_remove_rp_realm(ctab, rp_realm);
219   rp_realm2=tr_comm_table_find_rp_realm(ctab, rp_realm_name);
220   if (rp_realm2!=NULL) {
221     printf("RP realm %s still in table after removal.\n", entries[n].id);
222     tr_rp_realm_free(rp_realm);
223     tr_free_name(rp_realm_name);
224     return 2;
225   }
226
227   tr_rp_realm_free(rp_realm);
228   tr_free_name(rp_realm_name);
229   return 0;
230 }
231
232 /**********************************************************************/
233 /* TR_AAA_SERVER test stuff */
234
235 struct aaa_entry {
236   const char *hostname; /* only supports one for testing right now */
237 };
238
239 static TR_AAA_SERVER *aaa_entry_to_aaa_server(TALLOC_CTX *mem_ctx, struct aaa_entry *ae)
240 {
241   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
242   TR_AAA_SERVER *aaa=tr_aaa_server_new(tmp_ctx, tr_new_name(ae->hostname));
243
244   if ((aaa==NULL) || (aaa->hostname==NULL))
245     aaa=NULL;
246   else
247     talloc_steal(mem_ctx, aaa);
248
249   talloc_free(tmp_ctx); 
250   return aaa;
251 }
252
253
254 /**********************************************************************/
255 /* TR_IDP_REALM test stuff */
256
257 struct idp_realm_entry {
258   const char *id;
259   struct aaa_entry *aaa_servers;
260   struct apc_entry *apcs;
261 };
262
263 static TR_IDP_REALM *idp_realm_entry_to_idp_realm(TALLOC_CTX *mem_ctx, struct idp_realm_entry *re)
264 {
265   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
266   TR_IDP_REALM *realm=NULL;
267
268   realm=tr_idp_realm_new(tmp_ctx);
269   if (realm!=NULL) {
270     tr_idp_realm_set_id(realm, tr_new_name(re->id));
271     realm->aaa_servers=aaa_entry_to_aaa_server(realm, re->aaa_servers);
272     if (realm->aaa_servers==NULL)
273       realm=NULL; /* still in tmp_ctx so will be freed */
274     else {
275       tr_idp_realm_set_apcs(realm, apc_entry_to_apc(tmp_ctx, re->apcs));
276       if (tr_idp_realm_get_apcs(realm)==NULL)
277         realm=NULL;
278     }
279   }
280
281   if (realm!=NULL)
282     talloc_steal(mem_ctx, realm);
283
284   talloc_free(tmp_ctx); 
285   return realm;
286 }
287
288 static int add_idp_realm_set(TR_COMM_TABLE *ctab, struct idp_realm_entry *entries)
289 {
290   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
291   struct idp_realm_entry *this=NULL;
292   TR_IDP_REALM *realm=NULL;
293   int rc=-1;
294
295   for (this=entries; this->id!=NULL; this++) {
296     realm=idp_realm_entry_to_idp_realm(tmp_ctx, this);
297     if (realm==NULL) {
298       printf("Error creating IDP realm %s.\n", this->id);
299       rc=1;
300       goto cleanup;
301     }
302     tr_comm_table_add_idp_realm(ctab, realm);
303   }
304   rc=0;
305
306 cleanup:
307   talloc_free(tmp_ctx);
308   return rc;
309 }
310
311 static int verify_idp_realm_set(TR_COMM_TABLE *ctab, struct idp_realm_entry *entries)
312 {
313   struct idp_realm_entry *this=NULL;
314   TR_IDP_REALM *idp_realm=NULL;
315   TR_NAME *this_id=NULL;
316
317   for (this=entries; this->id!=NULL; this++) {
318     this_id=tr_new_name(this->id);
319     idp_realm=tr_comm_table_find_idp_realm(ctab, this_id);
320     tr_free_name(this_id); this_id=NULL;
321     
322     if (idp_realm==NULL) {
323       printf("Error, IDP realm %s missing from community table.\n", this->id);
324       return -1;
325     }
326   }
327   return 0;
328 }
329
330 /* removes entry n from ctab */
331 static int remove_idp_realm_set_member(TR_COMM_TABLE *ctab, struct idp_realm_entry *entries, size_t n)
332 {
333   TR_NAME *idp_realm_name=tr_new_name(entries[n].id);
334   TR_IDP_REALM *idp_realm=tr_comm_table_find_idp_realm(ctab, idp_realm_name);
335   TR_IDP_REALM *idp_realm2=NULL;
336
337   if (idp_realm==NULL) {
338     printf("Can't remove IDP realm %s, not in table.\n", entries[n].id);
339     tr_free_name(idp_realm_name);
340     return 1;
341   }
342   
343   tr_comm_table_remove_idp_realm(ctab, idp_realm);
344   idp_realm2=tr_comm_table_find_idp_realm(ctab, idp_realm_name);
345   if (idp_realm2!=NULL) {
346     printf("IDP realm %s still in table after removal.\n", entries[n].id);
347     tr_idp_realm_free(idp_realm);
348     tr_free_name(idp_realm_name);
349     return 2;
350   }
351
352   tr_idp_realm_free(idp_realm);
353   tr_free_name(idp_realm_name);
354   return 0;
355 }
356
357 /**********************************************************************/
358 /* Community Membership test stuff */
359
360 struct comm_memb_entry {
361   TR_REALM_ROLE role;
362   const char *realm_name;
363   const char *comm_name;
364   const char *origin;
365   /* TODO: test provenance */
366 };
367
368 /* add an existing realm to an existing community (these must
369  * exist in the community table lists) */
370 static int add_comm_membership(TR_COMM_TABLE *ctab, struct comm_memb_entry *entry)
371 {
372   TR_NAME *comm_name=tr_new_name(entry->comm_name);
373   TR_NAME *realm_name=tr_new_name(entry->realm_name);
374   TR_COMM *comm=tr_comm_table_find_comm(ctab, comm_name);
375   TR_RP_REALM *rp_realm=(entry->role==TR_ROLE_RP)?(tr_comm_table_find_rp_realm(ctab, realm_name)):(NULL);
376   TR_IDP_REALM *idp_realm=(entry->role==TR_ROLE_IDP)?(tr_comm_table_find_idp_realm(ctab, realm_name)):(NULL);
377   json_t *prov=NULL;
378   
379   if ((comm==NULL) || ((rp_realm==NULL)&&(idp_realm==NULL)))
380     return 1;
381
382   prov=json_array();
383   if (entry->origin!=NULL)
384     json_array_append(prov, json_string(entry->origin));
385
386   switch (entry->role) {
387   case TR_ROLE_IDP:
388     tr_comm_add_idp_realm(ctab, comm, idp_realm, 0, prov, NULL); /* Expiry!? */
389     break;
390   case TR_ROLE_RP:
391     tr_comm_add_rp_realm(ctab, comm, rp_realm, 0, prov, NULL); /* Expiry!? */
392     break;
393   default:
394     return 2;
395   }
396   
397   return 0;
398 }
399
400 static int add_member_set(TR_COMM_TABLE *ctab, struct comm_memb_entry *entries)
401 {
402   struct comm_memb_entry *this=NULL;
403
404   for (this=entries; this->role!=TR_ROLE_UNKNOWN; this++) {
405     if (0!=add_comm_membership(ctab, this)) {
406       printf("Error adding %s realm %s to community %s (origin %s).\n",
407              (this->role==TR_ROLE_RP)?"RP":"IDP",
408              this->realm_name,
409              this->comm_name,
410              (this->origin!=NULL)?(this->origin):"null");
411       return 1;
412     }
413   }
414   return 0;
415 }
416
417 static int remove_membership(TR_COMM_TABLE *ctab, struct comm_memb_entry *entries, size_t n)
418 {
419   TR_NAME *realm_name=tr_new_name(entries[n].realm_name);
420   TR_NAME *comm_name=tr_new_name(entries[n].comm_name);
421   TR_NAME *origin=(entries[n].origin!=NULL)?(tr_new_name(entries[n].origin)):NULL;
422   TR_COMM_MEMB *memb=NULL;
423   int rc=-1;
424
425   switch (entries[n].role) {
426   case TR_ROLE_IDP:
427     memb=tr_comm_table_find_idp_memb_origin(ctab, realm_name, comm_name, origin);
428     break;
429   case TR_ROLE_RP:
430     memb=tr_comm_table_find_rp_memb_origin(ctab, realm_name, comm_name, origin);
431     break;
432   default:
433     rc=1;
434     goto cleanup;
435   }
436
437   if (memb==NULL) {
438     printf("%s realm %s not in comm %s from origin %s, can't remove membership.\n",
439            (entries[n].role==TR_ROLE_RP)?"RP":"IDP",
440            entries[n].realm_name,
441            entries[n].comm_name,
442            (entries[n].origin!=NULL)?(entries[n].origin):"null");
443     rc=2;
444     goto cleanup;
445   }
446   tr_comm_table_remove_memb(ctab, memb);
447   tr_comm_memb_free(memb);
448   rc=0;
449
450 cleanup:
451   tr_free_name(realm_name);
452   tr_free_name(comm_name);
453   if (origin!=NULL)
454     tr_free_name(origin);
455   return rc;
456 }
457
458 /**********************************************************************/
459 /* Test data */
460
461 struct apc_entry apc_1={ "apc" };
462
463 struct comm_entry comm_set_1[]={
464   { "apc", TR_COMM_APC, NULL },
465   { "comm 1", TR_COMM_COI, &apc_1 },
466   { "comm 2", TR_COMM_COI, &apc_1 },
467   { NULL }
468 };
469
470 struct rp_realm_entry rp_realm_set_1[]={
471   { "rp 1" },
472   { "rp 2" },
473   { "rp 3" },
474   { NULL }
475 };
476
477 struct aaa_entry aaa_1= { "aaa 1" };
478 struct aaa_entry aaa_2= { "aaa 2" };
479 struct aaa_entry aaa_3= { "aaa 3" };
480
481 struct idp_realm_entry idp_realm_set_1[]={
482   { "idp 1", &aaa_1, &apc_1 },
483   { "idp 2", &aaa_2, &apc_1 },
484   { "idp 3", &aaa_3, &apc_1 },
485   { NULL }
486 };
487
488 struct comm_memb_entry member_set_1[]={
489   { TR_ROLE_RP, "rp 1", "apc", NULL },
490   { TR_ROLE_RP, "rp 2", "apc", NULL },
491   { TR_ROLE_RP, "rp 3", "apc", NULL },
492   { TR_ROLE_IDP, "idp 1", "apc", NULL },
493   { TR_ROLE_IDP, "idp 2", "apc", NULL },
494   { TR_ROLE_IDP, "idp 3", "apc", NULL },
495   { TR_ROLE_RP, "rp 1", "comm 1", NULL },
496   { TR_ROLE_RP, "rp 2", "comm 1", NULL },
497   { TR_ROLE_RP, "rp 2", "comm 1", "peer 1" },
498   { TR_ROLE_RP, "rp 2", "comm 1", "peer 2" },
499   { TR_ROLE_IDP, "idp 1", "comm 1", NULL },
500   { TR_ROLE_IDP, "idp 1", "comm 1", "peer 1" },
501   { TR_ROLE_IDP, "idp 1", "comm 1", "peer 2" },
502   { TR_ROLE_IDP, "idp 2", "comm 1", NULL },
503   { TR_ROLE_RP, "rp 1", "comm 2", NULL },
504   { TR_ROLE_RP, "rp 2", "comm 2", NULL },
505   { TR_ROLE_RP, "rp 2", "comm 2", "peer 1" },
506   { TR_ROLE_RP, "rp 2", "comm 2", "peer 2" },
507   { TR_ROLE_IDP, "idp 1", "comm 2", NULL },
508   { TR_ROLE_IDP, "idp 1", "comm 2", "peer 1" },
509   { TR_ROLE_IDP, "idp 1", "comm 2", "peer 2" },
510   { TR_ROLE_IDP, "idp 2", "comm 2", NULL },
511   { TR_ROLE_UNKNOWN }
512 };
513
514
515 /**********************************************************************/
516 /* Test routines */
517
518 /* the first few tests here insert a few things into the community table (comms,
519  * rp_realms, or idp_realms), then verify that they're all there. They
520  * then remove them in various orders, put them back, try removing
521  * things that are not present, etc. */
522
523 static int community_test(void)
524 {
525   TALLOC_CTX *mem_ctx=talloc_new(NULL);
526   TR_COMM_TABLE *ctab=tr_comm_table_new(mem_ctx);
527
528   assert(0==tr_comm_table_size(ctab));
529
530   /* add communities */
531   assert(ctab!=NULL);
532   assert(0==add_comm_set(ctab, comm_set_1));
533   assert(3==tr_comm_table_size(ctab));
534   assert(0==verify_comm_set(ctab, comm_set_1));
535
536   /* remove */
537   assert(0==remove_comm_set_member(ctab, comm_set_1, 0));
538   assert(2==tr_comm_table_size(ctab));
539   assert(0==remove_comm_set_member(ctab, comm_set_1, 1));
540   assert(1==tr_comm_table_size(ctab));
541   assert(0==remove_comm_set_member(ctab, comm_set_1, 2));
542   assert(0==tr_comm_table_size(ctab));
543   
544   /* add communities */
545   assert(ctab!=NULL);
546   assert(0==add_comm_set(ctab, comm_set_1));
547   assert(3==tr_comm_table_size(ctab));
548   assert(0==verify_comm_set(ctab, comm_set_1));
549
550   /* remove */
551   assert(0==remove_comm_set_member(ctab, comm_set_1, 0));
552   assert(2==tr_comm_table_size(ctab));
553   assert(0==remove_comm_set_member(ctab, comm_set_1, 2));
554   assert(1==tr_comm_table_size(ctab));
555   assert(0==remove_comm_set_member(ctab, comm_set_1, 1));
556   assert(0==tr_comm_table_size(ctab));
557   
558   /* add communities */
559   assert(ctab!=NULL);
560   assert(0==add_comm_set(ctab, comm_set_1));
561   assert(3==tr_comm_table_size(ctab));
562   assert(0==verify_comm_set(ctab, comm_set_1));
563
564   /* remove */
565   assert(0==remove_comm_set_member(ctab, comm_set_1, 1));
566   assert(2==tr_comm_table_size(ctab));
567   assert(0==remove_comm_set_member(ctab, comm_set_1, 0));
568   assert(1==tr_comm_table_size(ctab));
569   assert(0==remove_comm_set_member(ctab, comm_set_1, 2));
570   assert(0==tr_comm_table_size(ctab));
571   
572   assert(1==remove_comm_set_member(ctab, comm_set_1, 1)); /* should not be in the table */
573   assert(0==tr_comm_table_size(ctab));
574
575   /* add communities */
576   assert(ctab!=NULL);
577   assert(0==add_comm_set(ctab, comm_set_1));
578   assert(3==tr_comm_table_size(ctab));
579   assert(0==verify_comm_set(ctab, comm_set_1));
580
581   /* remove */
582   assert(0==remove_comm_set_member(ctab, comm_set_1, 1));
583   assert(2==tr_comm_table_size(ctab));
584   assert(1==remove_comm_set_member(ctab, comm_set_1, 1)); /* should not be in the table */
585   assert(2==tr_comm_table_size(ctab));
586   assert(0==remove_comm_set_member(ctab, comm_set_1, 2));
587   assert(1==tr_comm_table_size(ctab));
588   assert(0==remove_comm_set_member(ctab, comm_set_1, 0));
589   assert(0==tr_comm_table_size(ctab));
590   
591   /* add communities */
592   assert(ctab!=NULL);
593   assert(0==add_comm_set(ctab, comm_set_1));
594   assert(3==tr_comm_table_size(ctab));
595   assert(0==verify_comm_set(ctab, comm_set_1));
596
597   /* remove */
598   assert(0==remove_comm_set_member(ctab, comm_set_1, 2));
599   assert(2==tr_comm_table_size(ctab));
600   assert(0==remove_comm_set_member(ctab, comm_set_1, 0));
601   assert(1==tr_comm_table_size(ctab));
602   assert(0==remove_comm_set_member(ctab, comm_set_1, 1));
603   assert(0==tr_comm_table_size(ctab));
604   assert(1==remove_comm_set_member(ctab, comm_set_1, 1)); /* should not be in the table */
605   assert(0==tr_comm_table_size(ctab));
606   
607   /* add communities */
608   assert(ctab!=NULL);
609   assert(0==add_comm_set(ctab, comm_set_1));
610   assert(3==tr_comm_table_size(ctab));
611   assert(0==verify_comm_set(ctab, comm_set_1));
612
613   /* remove */
614   assert(0==remove_comm_set_member(ctab, comm_set_1, 2));
615   assert(2==tr_comm_table_size(ctab));
616   assert(0==remove_comm_set_member(ctab, comm_set_1, 1));
617   assert(1==tr_comm_table_size(ctab));
618   assert(1==remove_comm_set_member(ctab, comm_set_1, 1)); /* should not be in the table */
619   assert(1==tr_comm_table_size(ctab));
620   assert(0==remove_comm_set_member(ctab, comm_set_1, 0));
621   assert(0==tr_comm_table_size(ctab));
622
623   talloc_free(mem_ctx);
624   return 0;
625 }
626
627 static int rp_realm_test(void)
628 {
629   TALLOC_CTX *mem_ctx=talloc_new(NULL);
630   TR_COMM_TABLE *ctab=tr_comm_table_new(mem_ctx);
631
632   /* add realms */
633   assert(ctab!=NULL);
634   assert(0==add_rp_realm_set(ctab, rp_realm_set_1));
635   assert(0==verify_rp_realm_set(ctab, rp_realm_set_1));
636
637   /* remove */
638   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 0));
639   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 1));
640   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 2));
641   
642   /* add realms */
643   assert(ctab!=NULL);
644   assert(0==add_rp_realm_set(ctab, rp_realm_set_1));
645   assert(0==verify_rp_realm_set(ctab, rp_realm_set_1));
646
647   /* remove */
648   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 0));
649   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 2));
650   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 1));
651   
652   /* add realms */
653   assert(ctab!=NULL);
654   assert(0==add_rp_realm_set(ctab, rp_realm_set_1));
655   assert(0==verify_rp_realm_set(ctab, rp_realm_set_1));
656
657   /* remove */
658   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 1));
659   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 0));
660   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 2));
661   
662   assert(1==remove_rp_realm_set_member(ctab, rp_realm_set_1, 1)); /* should not be in the table */
663
664   /* add realms */
665   assert(ctab!=NULL);
666   assert(0==add_rp_realm_set(ctab, rp_realm_set_1));
667   assert(0==verify_rp_realm_set(ctab, rp_realm_set_1));
668
669   /* remove */
670   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 1));
671   assert(1==remove_rp_realm_set_member(ctab, rp_realm_set_1, 1)); /* should not be in the table */
672   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 2));
673   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 0));
674   
675   /* add realms */
676   assert(ctab!=NULL);
677   assert(0==add_rp_realm_set(ctab, rp_realm_set_1));
678   assert(0==verify_rp_realm_set(ctab, rp_realm_set_1));
679
680   /* remove */
681   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 2));
682   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 0));
683   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 1));
684   assert(1==remove_rp_realm_set_member(ctab, rp_realm_set_1, 1)); /* should not be in the table */
685   
686   /* add realms */
687   assert(ctab!=NULL);
688   assert(0==add_rp_realm_set(ctab, rp_realm_set_1));
689   assert(0==verify_rp_realm_set(ctab, rp_realm_set_1));
690
691   /* remove */
692   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 2));
693   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 1));
694   assert(1==remove_rp_realm_set_member(ctab, rp_realm_set_1, 1)); /* should not be in the table */
695   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 0));
696
697   talloc_free(mem_ctx);
698   return 0;
699 }
700
701 static int idp_realm_test(void)
702 {
703   TALLOC_CTX *mem_ctx=talloc_new(NULL);
704   TR_COMM_TABLE *ctab=tr_comm_table_new(mem_ctx);
705
706   /* add realms */
707   assert(ctab!=NULL);
708   assert(0==add_idp_realm_set(ctab, idp_realm_set_1));
709   assert(0==verify_idp_realm_set(ctab, idp_realm_set_1));
710
711   /* remove */
712   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 0));
713   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 1));
714   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 2));
715   
716   /* add realms */
717   assert(ctab!=NULL);
718   assert(0==add_idp_realm_set(ctab, idp_realm_set_1));
719   assert(0==verify_idp_realm_set(ctab, idp_realm_set_1));
720
721   /* remove */
722   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 0));
723   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 2));
724   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 1));
725   
726   /* add realms */
727   assert(ctab!=NULL);
728   assert(0==add_idp_realm_set(ctab, idp_realm_set_1));
729   assert(0==verify_idp_realm_set(ctab, idp_realm_set_1));
730
731   /* remove */
732   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 1));
733   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 0));
734   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 2));
735   
736   assert(1==remove_idp_realm_set_member(ctab, idp_realm_set_1, 1)); /* should not be in the table */
737
738   /* add realms */
739   assert(ctab!=NULL);
740   assert(0==add_idp_realm_set(ctab, idp_realm_set_1));
741   assert(0==verify_idp_realm_set(ctab, idp_realm_set_1));
742
743   /* remove */
744   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 1));
745   assert(1==remove_idp_realm_set_member(ctab, idp_realm_set_1, 1)); /* should not be in the table */
746   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 2));
747   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 0));
748   
749   /* add realms */
750   assert(ctab!=NULL);
751   assert(0==add_idp_realm_set(ctab, idp_realm_set_1));
752   assert(0==verify_idp_realm_set(ctab, idp_realm_set_1));
753
754   /* remove */
755   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 2));
756   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 0));
757   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 1));
758   assert(1==remove_idp_realm_set_member(ctab, idp_realm_set_1, 1)); /* should not be in the table */
759   
760   /* add realms */
761   assert(ctab!=NULL);
762   assert(0==add_idp_realm_set(ctab, idp_realm_set_1));
763   assert(0==verify_idp_realm_set(ctab, idp_realm_set_1));
764
765   /* remove */
766   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 2));
767   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 1));
768   assert(1==remove_idp_realm_set_member(ctab, idp_realm_set_1, 1)); /* should not be in the table */
769   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 0));
770
771   talloc_free(mem_ctx);
772   return 0;
773 }
774
775 static int membership_test(void)
776 {
777   TALLOC_CTX *mem_ctx=talloc_new(NULL);
778   TR_COMM_TABLE *ctab=tr_comm_table_new(mem_ctx);
779   size_t ii=0;
780   size_t size=0;
781
782   assert(ctab!=NULL);
783   assert(0==add_comm_set(ctab, comm_set_1));
784   assert(0==add_rp_realm_set(ctab, rp_realm_set_1));
785   assert(0==add_idp_realm_set(ctab, idp_realm_set_1));
786   assert(0==add_member_set(ctab, member_set_1));
787
788   size=tr_comm_table_size(ctab);
789   tr_comm_table_sweep(ctab);
790   assert(size==tr_comm_table_size(ctab));
791
792   /* now remove memberships */
793   for (ii=0; member_set_1[ii].role!=TR_ROLE_UNKNOWN; ii++) {
794     assert(0==remove_membership(ctab, member_set_1, ii));
795     assert(2==remove_membership(ctab, member_set_1, ii)); /* should not be in the table */
796   }
797
798   assert(NULL==ctab->memberships);
799
800   /* put them back */
801   assert(0==add_member_set(ctab, member_set_1));
802   /* tr_comm_table_print(stdout, ctab); */
803
804   tr_comm_table_sweep(ctab);
805   assert(size==tr_comm_table_size(ctab));
806
807   /* remove in the reverse order */
808   for(; ii>0; ii--) {
809     assert(0==remove_membership(ctab, member_set_1, ii-1));
810     assert(2==remove_membership(ctab, member_set_1, ii-1)); /* should not be in the table */
811     /* tr_comm_table_print(stdout, ctab); */
812   }
813
814   assert(NULL==ctab->memberships);
815
816   assert(size==tr_comm_table_size(ctab));
817   tr_comm_table_sweep(ctab);
818   assert(0==tr_comm_table_size(ctab));
819
820   talloc_free(mem_ctx);
821   return 0;
822 }
823
824
825 /**********************************************************************/
826 /* main */
827 int main(void)
828 {
829   assert(0==community_test());
830   printf("Community tests passed.\n");
831   assert(0==rp_realm_test());
832   printf("RP realm tests passed.\n");
833   assert(0==idp_realm_test());
834   printf("IDP realm tests passed.\n");
835   assert(0==membership_test());
836   printf("Membership tests passed.\n");
837   return 0;
838 }