Merge pull request #86 from painless-security/jennifer/aaa_server_port
[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_from_string(tmp_ctx, ae->hostname);
243
244   if (aaa)
245     talloc_steal(mem_ctx, aaa);
246
247   talloc_free(tmp_ctx); 
248   return aaa;
249 }
250
251
252 /**********************************************************************/
253 /* TR_IDP_REALM test stuff */
254
255 struct idp_realm_entry {
256   const char *id;
257   struct aaa_entry *aaa_servers;
258   struct apc_entry *apcs;
259 };
260
261 static TR_IDP_REALM *idp_realm_entry_to_idp_realm(TALLOC_CTX *mem_ctx, struct idp_realm_entry *re)
262 {
263   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
264   TR_IDP_REALM *realm=NULL;
265
266   realm=tr_idp_realm_new(tmp_ctx);
267   if (realm!=NULL) {
268     tr_idp_realm_set_id(realm, tr_new_name(re->id));
269     realm->aaa_servers=aaa_entry_to_aaa_server(realm, re->aaa_servers);
270     if (realm->aaa_servers==NULL)
271       realm=NULL; /* still in tmp_ctx so will be freed */
272     else {
273       tr_idp_realm_set_apcs(realm, apc_entry_to_apc(tmp_ctx, re->apcs));
274       if (tr_idp_realm_get_apcs(realm)==NULL)
275         realm=NULL;
276     }
277   }
278
279   if (realm!=NULL)
280     talloc_steal(mem_ctx, realm);
281
282   talloc_free(tmp_ctx); 
283   return realm;
284 }
285
286 static int add_idp_realm_set(TR_COMM_TABLE *ctab, struct idp_realm_entry *entries)
287 {
288   TALLOC_CTX *tmp_ctx=talloc_new(NULL);
289   struct idp_realm_entry *this=NULL;
290   TR_IDP_REALM *realm=NULL;
291   int rc=-1;
292
293   for (this=entries; this->id!=NULL; this++) {
294     realm=idp_realm_entry_to_idp_realm(tmp_ctx, this);
295     if (realm==NULL) {
296       printf("Error creating IDP realm %s.\n", this->id);
297       rc=1;
298       goto cleanup;
299     }
300     tr_comm_table_add_idp_realm(ctab, realm);
301   }
302   rc=0;
303
304 cleanup:
305   talloc_free(tmp_ctx);
306   return rc;
307 }
308
309 static int verify_idp_realm_set(TR_COMM_TABLE *ctab, struct idp_realm_entry *entries)
310 {
311   struct idp_realm_entry *this=NULL;
312   TR_IDP_REALM *idp_realm=NULL;
313   TR_NAME *this_id=NULL;
314
315   for (this=entries; this->id!=NULL; this++) {
316     this_id=tr_new_name(this->id);
317     idp_realm=tr_comm_table_find_idp_realm(ctab, this_id);
318     tr_free_name(this_id); this_id=NULL;
319     
320     if (idp_realm==NULL) {
321       printf("Error, IDP realm %s missing from community table.\n", this->id);
322       return -1;
323     }
324   }
325   return 0;
326 }
327
328 /* removes entry n from ctab */
329 static int remove_idp_realm_set_member(TR_COMM_TABLE *ctab, struct idp_realm_entry *entries, size_t n)
330 {
331   TR_NAME *idp_realm_name=tr_new_name(entries[n].id);
332   TR_IDP_REALM *idp_realm=tr_comm_table_find_idp_realm(ctab, idp_realm_name);
333   TR_IDP_REALM *idp_realm2=NULL;
334
335   if (idp_realm==NULL) {
336     printf("Can't remove IDP realm %s, not in table.\n", entries[n].id);
337     tr_free_name(idp_realm_name);
338     return 1;
339   }
340   
341   tr_comm_table_remove_idp_realm(ctab, idp_realm);
342   idp_realm2=tr_comm_table_find_idp_realm(ctab, idp_realm_name);
343   if (idp_realm2!=NULL) {
344     printf("IDP realm %s still in table after removal.\n", entries[n].id);
345     tr_idp_realm_free(idp_realm);
346     tr_free_name(idp_realm_name);
347     return 2;
348   }
349
350   tr_idp_realm_free(idp_realm);
351   tr_free_name(idp_realm_name);
352   return 0;
353 }
354
355 /**********************************************************************/
356 /* Community Membership test stuff */
357
358 struct comm_memb_entry {
359   TR_REALM_ROLE role;
360   const char *realm_name;
361   const char *comm_name;
362   const char *origin;
363   /* TODO: test provenance */
364 };
365
366 /* add an existing realm to an existing community (these must
367  * exist in the community table lists) */
368 static int add_comm_membership(TR_COMM_TABLE *ctab, struct comm_memb_entry *entry)
369 {
370   TR_NAME *comm_name=tr_new_name(entry->comm_name);
371   TR_NAME *realm_name=tr_new_name(entry->realm_name);
372   TR_COMM *comm=tr_comm_table_find_comm(ctab, comm_name);
373   TR_RP_REALM *rp_realm=(entry->role==TR_ROLE_RP)?(tr_comm_table_find_rp_realm(ctab, realm_name)):(NULL);
374   TR_IDP_REALM *idp_realm=(entry->role==TR_ROLE_IDP)?(tr_comm_table_find_idp_realm(ctab, realm_name)):(NULL);
375   json_t *prov=NULL;
376   
377   if ((comm==NULL) || ((rp_realm==NULL)&&(idp_realm==NULL)))
378     return 1;
379
380   prov=json_array();
381   if (entry->origin!=NULL)
382     json_array_append(prov, json_string(entry->origin));
383
384   switch (entry->role) {
385   case TR_ROLE_IDP:
386     tr_comm_add_idp_realm(ctab, comm, idp_realm, 0, prov, NULL); /* Expiry!? */
387     break;
388   case TR_ROLE_RP:
389     tr_comm_add_rp_realm(ctab, comm, rp_realm, 0, prov, NULL); /* Expiry!? */
390     break;
391   default:
392     return 2;
393   }
394   
395   return 0;
396 }
397
398 static int add_member_set(TR_COMM_TABLE *ctab, struct comm_memb_entry *entries)
399 {
400   struct comm_memb_entry *this=NULL;
401
402   for (this=entries; this->role!=TR_ROLE_UNKNOWN; this++) {
403     if (0!=add_comm_membership(ctab, this)) {
404       printf("Error adding %s realm %s to community %s (origin %s).\n",
405              (this->role==TR_ROLE_RP)?"RP":"IDP",
406              this->realm_name,
407              this->comm_name,
408              (this->origin!=NULL)?(this->origin):"null");
409       return 1;
410     }
411   }
412   return 0;
413 }
414
415 static int remove_membership(TR_COMM_TABLE *ctab, struct comm_memb_entry *entries, size_t n)
416 {
417   TR_NAME *realm_name=tr_new_name(entries[n].realm_name);
418   TR_NAME *comm_name=tr_new_name(entries[n].comm_name);
419   TR_NAME *origin=(entries[n].origin!=NULL)?(tr_new_name(entries[n].origin)):NULL;
420   TR_COMM_MEMB *memb=NULL;
421   int rc=-1;
422
423   switch (entries[n].role) {
424   case TR_ROLE_IDP:
425     memb=tr_comm_table_find_idp_memb_origin(ctab, realm_name, comm_name, origin);
426     break;
427   case TR_ROLE_RP:
428     memb=tr_comm_table_find_rp_memb_origin(ctab, realm_name, comm_name, origin);
429     break;
430   default:
431     rc=1;
432     goto cleanup;
433   }
434
435   if (memb==NULL) {
436     printf("%s realm %s not in comm %s from origin %s, can't remove membership.\n",
437            (entries[n].role==TR_ROLE_RP)?"RP":"IDP",
438            entries[n].realm_name,
439            entries[n].comm_name,
440            (entries[n].origin!=NULL)?(entries[n].origin):"null");
441     rc=2;
442     goto cleanup;
443   }
444   tr_comm_table_remove_memb(ctab, memb);
445   tr_comm_memb_free(memb);
446   rc=0;
447
448 cleanup:
449   tr_free_name(realm_name);
450   tr_free_name(comm_name);
451   if (origin!=NULL)
452     tr_free_name(origin);
453   return rc;
454 }
455
456 /**********************************************************************/
457 /* Test data */
458
459 struct apc_entry apc_1={ "apc" };
460
461 struct comm_entry comm_set_1[]={
462   { "apc", TR_COMM_APC, NULL },
463   { "comm 1", TR_COMM_COI, &apc_1 },
464   { "comm 2", TR_COMM_COI, &apc_1 },
465   { NULL }
466 };
467
468 struct rp_realm_entry rp_realm_set_1[]={
469   { "rp 1" },
470   { "rp 2" },
471   { "rp 3" },
472   { NULL }
473 };
474
475 struct aaa_entry aaa_1= { "aaa 1" };
476 struct aaa_entry aaa_2= { "aaa 2" };
477 struct aaa_entry aaa_3= { "aaa 3" };
478
479 struct idp_realm_entry idp_realm_set_1[]={
480   { "idp 1", &aaa_1, &apc_1 },
481   { "idp 2", &aaa_2, &apc_1 },
482   { "idp 3", &aaa_3, &apc_1 },
483   { NULL }
484 };
485
486 struct comm_memb_entry member_set_1[]={
487   { TR_ROLE_RP, "rp 1", "apc", NULL },
488   { TR_ROLE_RP, "rp 2", "apc", NULL },
489   { TR_ROLE_RP, "rp 3", "apc", NULL },
490   { TR_ROLE_IDP, "idp 1", "apc", NULL },
491   { TR_ROLE_IDP, "idp 2", "apc", NULL },
492   { TR_ROLE_IDP, "idp 3", "apc", NULL },
493   { TR_ROLE_RP, "rp 1", "comm 1", NULL },
494   { TR_ROLE_RP, "rp 2", "comm 1", NULL },
495   { TR_ROLE_RP, "rp 2", "comm 1", "peer 1" },
496   { TR_ROLE_RP, "rp 2", "comm 1", "peer 2" },
497   { TR_ROLE_IDP, "idp 1", "comm 1", NULL },
498   { TR_ROLE_IDP, "idp 1", "comm 1", "peer 1" },
499   { TR_ROLE_IDP, "idp 1", "comm 1", "peer 2" },
500   { TR_ROLE_IDP, "idp 2", "comm 1", NULL },
501   { TR_ROLE_RP, "rp 1", "comm 2", NULL },
502   { TR_ROLE_RP, "rp 2", "comm 2", NULL },
503   { TR_ROLE_RP, "rp 2", "comm 2", "peer 1" },
504   { TR_ROLE_RP, "rp 2", "comm 2", "peer 2" },
505   { TR_ROLE_IDP, "idp 1", "comm 2", NULL },
506   { TR_ROLE_IDP, "idp 1", "comm 2", "peer 1" },
507   { TR_ROLE_IDP, "idp 1", "comm 2", "peer 2" },
508   { TR_ROLE_IDP, "idp 2", "comm 2", NULL },
509   { TR_ROLE_UNKNOWN }
510 };
511
512
513 /**********************************************************************/
514 /* Test routines */
515
516 /* the first few tests here insert a few things into the community table (comms,
517  * rp_realms, or idp_realms), then verify that they're all there. They
518  * then remove them in various orders, put them back, try removing
519  * things that are not present, etc. */
520
521 static int community_test(void)
522 {
523   TALLOC_CTX *mem_ctx=talloc_new(NULL);
524   TR_COMM_TABLE *ctab=tr_comm_table_new(mem_ctx);
525
526   assert(0==tr_comm_table_size(ctab));
527
528   /* add communities */
529   assert(ctab!=NULL);
530   assert(0==add_comm_set(ctab, comm_set_1));
531   assert(3==tr_comm_table_size(ctab));
532   assert(0==verify_comm_set(ctab, comm_set_1));
533
534   /* remove */
535   assert(0==remove_comm_set_member(ctab, comm_set_1, 0));
536   assert(2==tr_comm_table_size(ctab));
537   assert(0==remove_comm_set_member(ctab, comm_set_1, 1));
538   assert(1==tr_comm_table_size(ctab));
539   assert(0==remove_comm_set_member(ctab, comm_set_1, 2));
540   assert(0==tr_comm_table_size(ctab));
541   
542   /* add communities */
543   assert(ctab!=NULL);
544   assert(0==add_comm_set(ctab, comm_set_1));
545   assert(3==tr_comm_table_size(ctab));
546   assert(0==verify_comm_set(ctab, comm_set_1));
547
548   /* remove */
549   assert(0==remove_comm_set_member(ctab, comm_set_1, 0));
550   assert(2==tr_comm_table_size(ctab));
551   assert(0==remove_comm_set_member(ctab, comm_set_1, 2));
552   assert(1==tr_comm_table_size(ctab));
553   assert(0==remove_comm_set_member(ctab, comm_set_1, 1));
554   assert(0==tr_comm_table_size(ctab));
555   
556   /* add communities */
557   assert(ctab!=NULL);
558   assert(0==add_comm_set(ctab, comm_set_1));
559   assert(3==tr_comm_table_size(ctab));
560   assert(0==verify_comm_set(ctab, comm_set_1));
561
562   /* remove */
563   assert(0==remove_comm_set_member(ctab, comm_set_1, 1));
564   assert(2==tr_comm_table_size(ctab));
565   assert(0==remove_comm_set_member(ctab, comm_set_1, 0));
566   assert(1==tr_comm_table_size(ctab));
567   assert(0==remove_comm_set_member(ctab, comm_set_1, 2));
568   assert(0==tr_comm_table_size(ctab));
569   
570   assert(1==remove_comm_set_member(ctab, comm_set_1, 1)); /* should not be in the table */
571   assert(0==tr_comm_table_size(ctab));
572
573   /* add communities */
574   assert(ctab!=NULL);
575   assert(0==add_comm_set(ctab, comm_set_1));
576   assert(3==tr_comm_table_size(ctab));
577   assert(0==verify_comm_set(ctab, comm_set_1));
578
579   /* remove */
580   assert(0==remove_comm_set_member(ctab, comm_set_1, 1));
581   assert(2==tr_comm_table_size(ctab));
582   assert(1==remove_comm_set_member(ctab, comm_set_1, 1)); /* should not be in the table */
583   assert(2==tr_comm_table_size(ctab));
584   assert(0==remove_comm_set_member(ctab, comm_set_1, 2));
585   assert(1==tr_comm_table_size(ctab));
586   assert(0==remove_comm_set_member(ctab, comm_set_1, 0));
587   assert(0==tr_comm_table_size(ctab));
588   
589   /* add communities */
590   assert(ctab!=NULL);
591   assert(0==add_comm_set(ctab, comm_set_1));
592   assert(3==tr_comm_table_size(ctab));
593   assert(0==verify_comm_set(ctab, comm_set_1));
594
595   /* remove */
596   assert(0==remove_comm_set_member(ctab, comm_set_1, 2));
597   assert(2==tr_comm_table_size(ctab));
598   assert(0==remove_comm_set_member(ctab, comm_set_1, 0));
599   assert(1==tr_comm_table_size(ctab));
600   assert(0==remove_comm_set_member(ctab, comm_set_1, 1));
601   assert(0==tr_comm_table_size(ctab));
602   assert(1==remove_comm_set_member(ctab, comm_set_1, 1)); /* should not be in the table */
603   assert(0==tr_comm_table_size(ctab));
604   
605   /* add communities */
606   assert(ctab!=NULL);
607   assert(0==add_comm_set(ctab, comm_set_1));
608   assert(3==tr_comm_table_size(ctab));
609   assert(0==verify_comm_set(ctab, comm_set_1));
610
611   /* remove */
612   assert(0==remove_comm_set_member(ctab, comm_set_1, 2));
613   assert(2==tr_comm_table_size(ctab));
614   assert(0==remove_comm_set_member(ctab, comm_set_1, 1));
615   assert(1==tr_comm_table_size(ctab));
616   assert(1==remove_comm_set_member(ctab, comm_set_1, 1)); /* should not be in the table */
617   assert(1==tr_comm_table_size(ctab));
618   assert(0==remove_comm_set_member(ctab, comm_set_1, 0));
619   assert(0==tr_comm_table_size(ctab));
620
621   talloc_free(mem_ctx);
622   return 0;
623 }
624
625 static int rp_realm_test(void)
626 {
627   TALLOC_CTX *mem_ctx=talloc_new(NULL);
628   TR_COMM_TABLE *ctab=tr_comm_table_new(mem_ctx);
629
630   /* add realms */
631   assert(ctab!=NULL);
632   assert(0==add_rp_realm_set(ctab, rp_realm_set_1));
633   assert(0==verify_rp_realm_set(ctab, rp_realm_set_1));
634
635   /* remove */
636   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 0));
637   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 1));
638   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 2));
639   
640   /* add realms */
641   assert(ctab!=NULL);
642   assert(0==add_rp_realm_set(ctab, rp_realm_set_1));
643   assert(0==verify_rp_realm_set(ctab, rp_realm_set_1));
644
645   /* remove */
646   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 0));
647   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 2));
648   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 1));
649   
650   /* add realms */
651   assert(ctab!=NULL);
652   assert(0==add_rp_realm_set(ctab, rp_realm_set_1));
653   assert(0==verify_rp_realm_set(ctab, rp_realm_set_1));
654
655   /* remove */
656   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 1));
657   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 0));
658   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 2));
659   
660   assert(1==remove_rp_realm_set_member(ctab, rp_realm_set_1, 1)); /* should not be in the table */
661
662   /* add realms */
663   assert(ctab!=NULL);
664   assert(0==add_rp_realm_set(ctab, rp_realm_set_1));
665   assert(0==verify_rp_realm_set(ctab, rp_realm_set_1));
666
667   /* remove */
668   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 1));
669   assert(1==remove_rp_realm_set_member(ctab, rp_realm_set_1, 1)); /* should not be in the table */
670   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 2));
671   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 0));
672   
673   /* add realms */
674   assert(ctab!=NULL);
675   assert(0==add_rp_realm_set(ctab, rp_realm_set_1));
676   assert(0==verify_rp_realm_set(ctab, rp_realm_set_1));
677
678   /* remove */
679   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 2));
680   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 0));
681   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 1));
682   assert(1==remove_rp_realm_set_member(ctab, rp_realm_set_1, 1)); /* should not be in the table */
683   
684   /* add realms */
685   assert(ctab!=NULL);
686   assert(0==add_rp_realm_set(ctab, rp_realm_set_1));
687   assert(0==verify_rp_realm_set(ctab, rp_realm_set_1));
688
689   /* remove */
690   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 2));
691   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 1));
692   assert(1==remove_rp_realm_set_member(ctab, rp_realm_set_1, 1)); /* should not be in the table */
693   assert(0==remove_rp_realm_set_member(ctab, rp_realm_set_1, 0));
694
695   talloc_free(mem_ctx);
696   return 0;
697 }
698
699 static int idp_realm_test(void)
700 {
701   TALLOC_CTX *mem_ctx=talloc_new(NULL);
702   TR_COMM_TABLE *ctab=tr_comm_table_new(mem_ctx);
703
704   /* add realms */
705   assert(ctab!=NULL);
706   assert(0==add_idp_realm_set(ctab, idp_realm_set_1));
707   assert(0==verify_idp_realm_set(ctab, idp_realm_set_1));
708
709   /* remove */
710   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 0));
711   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 1));
712   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 2));
713   
714   /* add realms */
715   assert(ctab!=NULL);
716   assert(0==add_idp_realm_set(ctab, idp_realm_set_1));
717   assert(0==verify_idp_realm_set(ctab, idp_realm_set_1));
718
719   /* remove */
720   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 0));
721   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 2));
722   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 1));
723   
724   /* add realms */
725   assert(ctab!=NULL);
726   assert(0==add_idp_realm_set(ctab, idp_realm_set_1));
727   assert(0==verify_idp_realm_set(ctab, idp_realm_set_1));
728
729   /* remove */
730   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 1));
731   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 0));
732   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 2));
733   
734   assert(1==remove_idp_realm_set_member(ctab, idp_realm_set_1, 1)); /* should not be in the table */
735
736   /* add realms */
737   assert(ctab!=NULL);
738   assert(0==add_idp_realm_set(ctab, idp_realm_set_1));
739   assert(0==verify_idp_realm_set(ctab, idp_realm_set_1));
740
741   /* remove */
742   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 1));
743   assert(1==remove_idp_realm_set_member(ctab, idp_realm_set_1, 1)); /* should not be in the table */
744   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 2));
745   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 0));
746   
747   /* add realms */
748   assert(ctab!=NULL);
749   assert(0==add_idp_realm_set(ctab, idp_realm_set_1));
750   assert(0==verify_idp_realm_set(ctab, idp_realm_set_1));
751
752   /* remove */
753   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 2));
754   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 0));
755   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 1));
756   assert(1==remove_idp_realm_set_member(ctab, idp_realm_set_1, 1)); /* should not be in the table */
757   
758   /* add realms */
759   assert(ctab!=NULL);
760   assert(0==add_idp_realm_set(ctab, idp_realm_set_1));
761   assert(0==verify_idp_realm_set(ctab, idp_realm_set_1));
762
763   /* remove */
764   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 2));
765   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 1));
766   assert(1==remove_idp_realm_set_member(ctab, idp_realm_set_1, 1)); /* should not be in the table */
767   assert(0==remove_idp_realm_set_member(ctab, idp_realm_set_1, 0));
768
769   talloc_free(mem_ctx);
770   return 0;
771 }
772
773 static int membership_test(void)
774 {
775   TALLOC_CTX *mem_ctx=talloc_new(NULL);
776   TR_COMM_TABLE *ctab=tr_comm_table_new(mem_ctx);
777   size_t ii=0;
778   size_t size=0;
779
780   assert(ctab!=NULL);
781   assert(0==add_comm_set(ctab, comm_set_1));
782   assert(0==add_rp_realm_set(ctab, rp_realm_set_1));
783   assert(0==add_idp_realm_set(ctab, idp_realm_set_1));
784   assert(0==add_member_set(ctab, member_set_1));
785
786   size=tr_comm_table_size(ctab);
787   tr_comm_table_sweep(ctab);
788   assert(size==tr_comm_table_size(ctab));
789
790   /* now remove memberships */
791   for (ii=0; member_set_1[ii].role!=TR_ROLE_UNKNOWN; ii++) {
792     assert(0==remove_membership(ctab, member_set_1, ii));
793     assert(2==remove_membership(ctab, member_set_1, ii)); /* should not be in the table */
794   }
795
796   assert(NULL==ctab->memberships);
797
798   /* put them back */
799   assert(0==add_member_set(ctab, member_set_1));
800   /* tr_comm_table_print(stdout, ctab); */
801
802   tr_comm_table_sweep(ctab);
803   assert(size==tr_comm_table_size(ctab));
804
805   /* remove in the reverse order */
806   for(; ii>0; ii--) {
807     assert(0==remove_membership(ctab, member_set_1, ii-1));
808     assert(2==remove_membership(ctab, member_set_1, ii-1)); /* should not be in the table */
809     /* tr_comm_table_print(stdout, ctab); */
810   }
811
812   assert(NULL==ctab->memberships);
813
814   assert(size==tr_comm_table_size(ctab));
815   tr_comm_table_sweep(ctab);
816   assert(0==tr_comm_table_size(ctab));
817
818   talloc_free(mem_ctx);
819   return 0;
820 }
821
822
823 /**********************************************************************/
824 /* main */
825 int main(void)
826 {
827   assert(0==community_test());
828   printf("Community tests passed.\n");
829   assert(0==rp_realm_test());
830   printf("RP realm tests passed.\n");
831   assert(0==idp_realm_test());
832   printf("IDP realm tests passed.\n");
833   assert(0==membership_test());
834   printf("Membership tests passed.\n");
835   return 0;
836 }