d8a0117896ca2b21983ad6dc0eaf4221da3e1ae0
[trust_router.git] / common / cfg_test / cfg_test.c
1 /*
2  * Copyright (c) 2016, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * 3. Neither the name of JANET(UK) nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31  * OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34
35 #include <stdio.h>
36 #include <talloc.h>
37 #include <assert.h>
38
39 #include <trust_router/tr_name.h>
40 #include <tr_comm.h>
41 #include <tr_idp.h>
42 #include <tr_config.h>
43 #include <tr_debug.h>
44
45 static void tr_talloc_log(const char *msg)
46 {
47   tr_debug("talloc: %s", msg);
48 }
49
50 static int verify_idp_cfg(TR_CFG *cfg)
51 {
52   TR_COMM *comm=NULL;
53   TR_NAME *name=NULL;
54   TR_APC *apc=NULL;
55   TR_IDP_REALM *idp_realm=NULL;
56   TR_AAA_SERVER *aaa=NULL;
57
58   assert(cfg!=NULL);
59
60   /* test the comms attribute */
61   assert(cfg->comms!=NULL);
62   name=tr_new_name("apc.example.com");
63   comm=tr_comm_lookup(cfg->comms, name);
64   tr_free_name(name);
65   assert(comm!=NULL);
66
67   assert(comm->type==TR_COMM_APC);
68   assert(comm->expiration_interval==TR_DEFAULT_APC_EXPIRATION_INTERVAL);
69   assert(comm->apcs==NULL);
70
71   name=tr_new_name("A.idp.cfg");
72   for (idp_realm=comm->idp_realms;
73        (idp_realm!=NULL) && (tr_name_cmp(name, idp_realm->realm_id)!=0);
74        idp_realm=idp_realm->comm_next) { }
75   assert(idp_realm!=NULL);
76   assert(idp_realm->shared_config==0);
77   assert(idp_realm->origin==TR_REALM_LOCAL);
78   tr_free_name(name);
79   name=tr_new_name("apc.example.com");
80   assert(tr_name_cmp(idp_realm->apcs->id, name)==0);
81   tr_free_name(name);
82   
83   assert(idp_realm->aaa_servers!=NULL);
84   name=tr_new_name("rad1.A.idp.cfg");
85   for (aaa=idp_realm->aaa_servers;
86        (aaa!=NULL) && (tr_name_cmp(name, aaa->hostname)!=0);
87        aaa=aaa->next) { }
88   assert(aaa!=NULL);
89   tr_free_name(name);
90
91   name=tr_new_name("rad2.A.idp.cfg");
92   for (aaa=idp_realm->aaa_servers;
93        (aaa!=NULL) && (tr_name_cmp(name, aaa->hostname)!=0);
94        aaa=aaa->next) { }
95   assert(aaa!=NULL);
96   tr_free_name(name);
97
98   return 0;
99 }
100
101 static int verify_rp_cfg(TR_CFG *cfg)
102 {
103   int ii=0;
104   TR_NAME *name=NULL;
105
106   assert(cfg!=NULL);
107   assert(cfg->rp_clients!=NULL);
108   assert(cfg->rp_clients->next==NULL);
109   assert(cfg->rp_clients->comm_next==NULL);
110   /* need to update next test to use TR_GSS_NAMES structure */
111 #if 0
112   for (ii=1; ii<TR_MAX_GSS_NAMES; ii++)
113     assert(cfg->rp_clients->gss_names[ii]==NULL);
114   assert(cfg->rp_clients->gss_names[0]!=NULL);
115   name=tr_new_name("gss@example.com");
116   assert(tr_name_cmp(name, cfg->rp_clients->gss_names[0])==0);
117 #endif
118   return 0;
119 }
120
121 int main(void)
122 {
123   TALLOC_CTX *mem_ctx=talloc_new(NULL);
124   TR_CFG *cfg=NULL;
125   TR_CFG_RC rc=TR_CFG_ERROR;
126
127   tr_log_open();
128
129   talloc_set_log_fn(tr_talloc_log);
130   cfg=tr_cfg_new(mem_ctx);
131
132   printf("Parsing idp.cfg.\n");
133   rc=tr_cfg_parse_one_config_file(cfg, "idp.cfg");
134   switch(rc) {
135   case TR_CFG_SUCCESS:
136     tr_debug("main: TR_CFG_SUCCESS");
137     break;
138   case TR_CFG_ERROR:
139     tr_debug("main: TR_CFG_ERROR");
140     break;
141   case TR_CFG_BAD_PARAMS:
142     tr_debug("main: TR_CFG_BAD_PARAMS");
143     break;
144   case TR_CFG_NOPARSE:
145     tr_debug("main: TR_CFG_NOPARSE");
146     break;
147   case TR_CFG_NOMEM:
148     tr_debug("main: TR_CFG_NOMEM");
149     break;
150   }
151
152   printf("Verifying IDP parse results... ");
153   if (verify_idp_cfg(cfg)!=0) {
154     printf("Error!\n");
155     exit(-1);
156   }
157   printf("success!\n");
158
159   printf("Verifying RP parse results... ");
160   if (verify_rp_cfg(cfg)!=0) {
161     printf("Error!\n");
162     exit(-1);
163   }
164   printf("success!\n");
165
166   talloc_report_full(mem_ctx, stderr);
167   tr_cfg_free(cfg);
168
169   printf("Cleared configuration for next test.\n\n");
170
171   cfg=tr_cfg_new(mem_ctx);
172   
173   printf("Parsing rp.cfg.\n");
174   rc=tr_cfg_parse_one_config_file(cfg, "rp.cfg");
175   switch(rc) {
176   case TR_CFG_SUCCESS:
177     tr_debug("main: TR_CFG_SUCCESS");
178     break;
179   case TR_CFG_ERROR:
180     tr_debug("main: TR_CFG_ERROR");
181     break;
182   case TR_CFG_BAD_PARAMS:
183     tr_debug("main: TR_CFG_BAD_PARAMS");
184     break;
185   case TR_CFG_NOPARSE:
186     tr_debug("main: TR_CFG_NOPARSE");
187     break;
188   case TR_CFG_NOMEM:
189     tr_debug("main: TR_CFG_NOMEM");
190     break;
191   }
192
193 #if 0
194   printf("Verifying RP parse results... ");
195   if (verify_rp_cfg(cfg)!=0) {
196     printf("Error!\n");
197     exit(-1);
198   }
199   printf("success!\n");
200 #endif
201
202   talloc_free(mem_ctx);
203   return 0;
204 }