Merge pull request #62 from painless-security/jennifer/report_incoming_ipaddr
[trust_router.git] / common / tests / 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 <tr_name_internal.h>
40 #include <tr_comm.h>
41 #include <tr_idp.h>
42 #include <tr_config.h>
43 #include <tr_debug.h>
44 #include <tr_gss_names.h>
45
46 static void tr_talloc_log(const char *msg)
47 {
48   tr_debug("talloc: %s", msg);
49 }
50
51 static int verify_idp_cfg(TR_CFG *cfg)
52 {
53   TR_COMM *comm=NULL;
54   TR_NAME *name=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->ctable!=NULL);
62   name=tr_new_name("apc.example.com");
63   comm=tr_comm_table_find_comm(cfg->ctable, 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   assert(name!=NULL);
73   idp_realm=tr_comm_find_idp(cfg->ctable, comm, name);
74   assert(idp_realm!=NULL);
75   assert(idp_realm->shared_config==0);
76   assert(idp_realm->origin==TR_REALM_LOCAL);
77   tr_free_name(name);
78   name=tr_new_name("apc.example.com");
79   assert(tr_name_cmp(idp_realm->apcs->id, name)==0);
80   tr_free_name(name);
81   
82   assert(idp_realm->aaa_servers!=NULL);
83   name=tr_new_name("rad1.A.idp.cfg");
84   for (aaa=idp_realm->aaa_servers;
85        (aaa!=NULL) && (tr_name_cmp(name, aaa->hostname)!=0);
86        aaa=aaa->next) { }
87   assert(aaa!=NULL);
88   tr_free_name(name);
89
90   name=tr_new_name("rad2.A.idp.cfg");
91   for (aaa=idp_realm->aaa_servers;
92        (aaa!=NULL) && (tr_name_cmp(name, aaa->hostname)!=0);
93        aaa=aaa->next) { }
94   assert(aaa!=NULL);
95   tr_free_name(name);
96
97   return 0;
98 }
99
100 static int verify_rp_cfg(TR_CFG *cfg)
101 {
102   TR_NAME *name=NULL;
103
104   assert(cfg!=NULL);
105   assert(cfg->rp_clients!=NULL);
106   assert(cfg->rp_clients->next==NULL);
107   assert(cfg->rp_clients->comm_next==NULL);
108
109   assert(cfg->rp_clients->gss_names!=NULL);
110   assert(tr_gss_names_length(cfg->rp_clients->gss_names) == 1);
111   name=tr_new_name("gss@example.com");
112   assert(tr_name_cmp(name, tr_gss_names_index(cfg->rp_clients->gss_names, 0))==0);
113   return 0;
114 }
115
116 int main(void)
117 {
118   TALLOC_CTX *mem_ctx=talloc_new(NULL);
119   TR_CFG_MGR *cfg_mgr=NULL;
120   TR_CFG_RC rc=TR_CFG_ERROR;
121   char *fname=NULL;
122
123   tr_log_open();
124
125   talloc_set_log_fn(tr_talloc_log);
126   cfg_mgr=tr_cfg_mgr_new(mem_ctx);
127
128   printf("Parsing idp.cfg.\n");
129   fname="idp.cfg";
130   rc=tr_parse_config(cfg_mgr, 1, &fname);
131   switch(rc) {
132   case TR_CFG_SUCCESS:
133     tr_debug("main: TR_CFG_SUCCESS");
134     break;
135   case TR_CFG_ERROR:
136     tr_debug("main: TR_CFG_ERROR");
137     break;
138   case TR_CFG_BAD_PARAMS:
139     tr_debug("main: TR_CFG_BAD_PARAMS");
140     break;
141   case TR_CFG_NOPARSE:
142     tr_debug("main: TR_CFG_NOPARSE");
143     break;
144   case TR_CFG_NOMEM:
145     tr_debug("main: TR_CFG_NOMEM");
146     break;
147   }
148
149   printf("Verifying IDP parse results... ");
150   if (verify_idp_cfg(cfg_mgr->new)!=0) {
151     printf("Error!\n");
152     exit(-1);
153   }
154   printf("success!\n");
155
156   printf("Verifying RP parse results... ");
157   if (verify_rp_cfg(cfg_mgr->new)!=0) {
158     printf("Error!\n");
159     exit(-1);
160   }
161   printf("success!\n");
162
163   talloc_report_full(mem_ctx, stderr);
164   tr_cfg_mgr_free(cfg_mgr);
165   talloc_free(mem_ctx);
166   return 0;
167 }