896b8ac647b40b96e07e2dbff1e002882b3271ec
[trust_router.git] / common / cfg_test / cfg_test.c
1 /* test suite for tr_config.c */
2 #include <stdio.h>
3 #include <talloc.h>
4 #include <assert.h>
5
6 #include <trust_router/tr_name.h>
7 #include <tr_comm.h>
8 #include <tr_idp.h>
9 #include <tr_config.h>
10 #include <tr_debug.h>
11
12 static void tr_talloc_log(const char *msg)
13 {
14   tr_debug("talloc: %s", msg);
15 }
16
17 static int verify_idp_cfg(TR_CFG *cfg)
18 {
19   TR_COMM *comm=NULL;
20   TR_NAME *name=NULL;
21   TR_APC *apc=NULL;
22   TR_IDP_REALM *idp_realm=NULL;
23   TR_AAA_SERVER *aaa=NULL;
24
25   assert(cfg!=NULL);
26
27   /* test the comms attribute */
28   assert(cfg->comms!=NULL);
29   name=tr_new_name("apc.example.com");
30   comm=tr_comm_lookup(cfg->comms, name);
31   tr_free_name(name);
32   assert(comm!=NULL);
33
34   assert(comm->type==TR_COMM_APC);
35   assert(comm->expiration_interval==TR_DEFAULT_APC_EXPIRATION_INTERVAL);
36   assert(comm->apcs==NULL);
37
38   name=tr_new_name("A.idp.cfg");
39   for (idp_realm=comm->idp_realms;
40        (idp_realm!=NULL) && (tr_name_cmp(name, idp_realm->realm_id)!=0);
41        idp_realm=idp_realm->comm_next) { }
42   assert(idp_realm!=NULL);
43   assert(idp_realm->shared_config==0);
44   assert(idp_realm->origin==TR_REALM_LOCAL);
45   tr_free_name(name);
46   name=tr_new_name("apc.example.com");
47   assert(tr_name_cmp(idp_realm->apcs->id, name)==0);
48   tr_free_name(name);
49   
50   assert(idp_realm->aaa_servers!=NULL);
51   name=tr_new_name("rad1.A.idp.cfg");
52   for (aaa=idp_realm->aaa_servers;
53        (aaa!=NULL) && (tr_name_cmp(name, aaa->hostname)!=0);
54        aaa=aaa->next) { }
55   assert(aaa!=NULL);
56   tr_free_name(name);
57
58   name=tr_new_name("rad2.A.idp.cfg");
59   for (aaa=idp_realm->aaa_servers;
60        (aaa!=NULL) && (tr_name_cmp(name, aaa->hostname)!=0);
61        aaa=aaa->next) { }
62   assert(aaa!=NULL);
63   tr_free_name(name);
64
65   return 0;
66 }
67
68 int main(void)
69 {
70   TALLOC_CTX *mem_ctx=talloc_new(NULL);
71   TR_CFG *cfg=NULL;
72   TR_CFG_RC rc=TR_CFG_ERROR;
73
74   tr_log_open();
75
76   talloc_set_log_fn(tr_talloc_log);
77   cfg=tr_cfg_new(mem_ctx);
78
79   printf("Parsing idp.cfg.\n");
80   rc=tr_cfg_parse_one_config_file(cfg, "idp.cfg");
81   switch(rc) {
82   case TR_CFG_SUCCESS:
83     tr_debug("main: TR_CFG_SUCCESS");
84     break;
85   case TR_CFG_ERROR:
86     tr_debug("main: TR_CFG_ERROR");
87     break;
88   case TR_CFG_BAD_PARAMS:
89     tr_debug("main: TR_CFG_BAD_PARAMS");
90     break;
91   case TR_CFG_NOPARSE:
92     tr_debug("main: TR_CFG_NOPARSE");
93     break;
94   case TR_CFG_NOMEM:
95     tr_debug("main: TR_CFG_NOMEM");
96     break;
97   }
98
99   printf("Verifying parse results... ");
100   if (verify_idp_cfg(cfg)!=0) {
101     printf("Error!\n");
102     exit(-1);
103   }
104   printf("success!\n");
105
106   talloc_report_full(mem_ctx, stderr);
107   tr_cfg_free(cfg);
108   return 0;
109 }