Reimplement TR_GSS_NAMES using GPtrArray
[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
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_IDP_REALM *idp_realm=NULL;
55   TR_AAA_SERVER *aaa=NULL;
56
57   assert(cfg!=NULL);
58
59   /* test the comms attribute */
60   assert(cfg->ctable!=NULL);
61   name=tr_new_name("apc.example.com");
62   comm=tr_comm_table_find_comm(cfg->ctable, name);
63   tr_free_name(name);
64   assert(comm!=NULL);
65
66   assert(comm->type==TR_COMM_APC);
67   assert(comm->expiration_interval==TR_DEFAULT_APC_EXPIRATION_INTERVAL);
68   assert(comm->apcs==NULL);
69
70   name=tr_new_name("A.idp.cfg");
71   assert(name!=NULL);
72   idp_realm=tr_comm_find_idp(cfg->ctable, comm, name);
73   assert(idp_realm!=NULL);
74   assert(idp_realm->shared_config==0);
75   assert(idp_realm->origin==TR_REALM_LOCAL);
76   tr_free_name(name);
77   name=tr_new_name("apc.example.com");
78   assert(tr_name_cmp(idp_realm->apcs->id, name)==0);
79   tr_free_name(name);
80   
81   assert(idp_realm->aaa_servers!=NULL);
82   name=tr_new_name("rad1.A.idp.cfg");
83   for (aaa=idp_realm->aaa_servers;
84        (aaa!=NULL) && (tr_name_cmp(name, aaa->hostname)!=0);
85        aaa=aaa->next) { }
86   assert(aaa!=NULL);
87   tr_free_name(name);
88
89   name=tr_new_name("rad2.A.idp.cfg");
90   for (aaa=idp_realm->aaa_servers;
91        (aaa!=NULL) && (tr_name_cmp(name, aaa->hostname)!=0);
92        aaa=aaa->next) { }
93   assert(aaa!=NULL);
94   tr_free_name(name);
95
96   return 0;
97 }
98
99 static int verify_rp_cfg(TR_CFG *cfg)
100 {
101   TR_NAME *name=NULL;
102
103   assert(cfg!=NULL);
104   assert(cfg->rp_clients!=NULL);
105   assert(cfg->rp_clients->next==NULL);
106   assert(cfg->rp_clients->comm_next==NULL);
107
108   assert(cfg->rp_clients->gss_names!=NULL);
109   assert(cfg->rp_clients->gss_names->names->len == 1);
110   name=tr_new_name("gss@example.com");
111   assert(tr_name_cmp(name, g_ptr_array_index(cfg->rp_clients->gss_names->names, 0))==0);
112   return 0;
113 }
114
115 int main(void)
116 {
117   TALLOC_CTX *mem_ctx=talloc_new(NULL);
118   TR_CFG_MGR *cfg_mgr=NULL;
119   TR_CFG_RC rc=TR_CFG_ERROR;
120   char *fname=NULL;
121
122   tr_log_open();
123
124   talloc_set_log_fn(tr_talloc_log);
125   cfg_mgr=tr_cfg_mgr_new(mem_ctx);
126
127   printf("Parsing idp.cfg.\n");
128   fname="idp.cfg";
129   rc=tr_parse_config(cfg_mgr, 1, &fname);
130   switch(rc) {
131   case TR_CFG_SUCCESS:
132     tr_debug("main: TR_CFG_SUCCESS");
133     break;
134   case TR_CFG_ERROR:
135     tr_debug("main: TR_CFG_ERROR");
136     break;
137   case TR_CFG_BAD_PARAMS:
138     tr_debug("main: TR_CFG_BAD_PARAMS");
139     break;
140   case TR_CFG_NOPARSE:
141     tr_debug("main: TR_CFG_NOPARSE");
142     break;
143   case TR_CFG_NOMEM:
144     tr_debug("main: TR_CFG_NOMEM");
145     break;
146   }
147
148   printf("Verifying IDP parse results... ");
149   if (verify_idp_cfg(cfg_mgr->new)!=0) {
150     printf("Error!\n");
151     exit(-1);
152   }
153   printf("success!\n");
154
155   printf("Verifying RP parse results... ");
156   if (verify_rp_cfg(cfg_mgr->new)!=0) {
157     printf("Error!\n");
158     exit(-1);
159   }
160   printf("success!\n");
161
162   talloc_report_full(mem_ctx, stderr);
163   tr_cfg_mgr_free(cfg_mgr);
164   talloc_free(mem_ctx);
165   return 0;
166 }