Add copyright statement missing from recently added files.
[trust_router.git] / trp / test / ptbl_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_gss.h>
40 #include <trp_internal.h>
41 #include <trp_ptable.h>
42
43
44 /* Can't do the updates test because trps_select_updates_for_peer() is now static */
45 #define VERIFY_UPDATES 0
46
47 struct peer_entry {
48   char *server;
49   unsigned int port;
50   unsigned int linkcost;
51 };
52
53 static struct peer_entry peer_data[]={
54   {"peer0", 10000, 0x0001},
55   {"peer1", 15000, 0x0002},
56   {"peer2", 20000, 0x0004},
57   {"peer3", 25000, 0x0008},
58   {"peer4", 30000, 0x0010}
59 };
60 static size_t n_peers=sizeof(peer_data)/sizeof(peer_data[0]);
61
62 static void populate_ptable(TRPS_INSTANCE *trps)
63 {
64   TRP_PEER *new_peer;
65   int i;
66
67   for (i=0; i<n_peers; i++) {
68     new_peer=trp_peer_new(NULL);
69     assert(new_peer!=NULL);
70     trp_peer_set_server(new_peer, peer_data[i].server);
71     assert(trp_peer_get_server(new_peer)!=NULL);
72     trp_peer_set_port(new_peer, peer_data[i].port);
73     trp_peer_set_linkcost(new_peer, peer_data[i].linkcost);
74     assert(trps_add_peer(trps, new_peer)==TRP_SUCCESS);
75   }
76 }
77
78 static struct peer_entry *find_peer_entry(char *server)
79 {
80   int i;
81   for (i=0; i<n_peers; i++) {
82     if (0==strcmp(server, peer_data[i].server)) {
83       return (peer_data+i);
84     }
85   }
86   return NULL;
87 }
88
89 static void verify_ptable(TRPS_INSTANCE *trps)
90 {
91   struct peer_entry *peer_entry=NULL;
92   TRP_PEER *peer;
93   char *s;
94   TR_NAME *gssname;
95
96   peer=trps->ptable->head;
97   while (peer!=NULL) {
98     peer_entry=find_peer_entry(trp_peer_get_server(peer));
99     assert(peer_entry!=NULL);
100     assert(!strcmp(trp_peer_get_server(peer), peer_entry->server));
101     assert(trp_peer_get_port(peer)==peer_entry->port);
102     assert(trp_peer_get_linkcost(peer)==peer_entry->linkcost);
103     assert(0<asprintf(&s, "trustrouter@%s", peer_entry->server));
104     gssname=tr_new_name(s);
105     free(s);
106     assert(gssname!=NULL);
107     assert(tr_gss_names_matches(trp_peer_get_gss_names(peer), gssname));
108     tr_free_name(gssname);
109     peer=peer->next;
110   }
111 }
112
113 struct route_data {
114   char *apc;
115   char *realm;
116   char *peer;
117   unsigned int metric;
118   char *trust_router;
119   char *next_hop;
120   int selected;
121   unsigned int interval;
122   int verified; /* for testing */
123 };
124 static struct route_data route_table[]={
125   {"apc0", "realm0", "", 0, "tr.r0.apc0", "", 1, 60, 0},
126   {"apc0", "realm1", "", 0, "tr.r1.apc0", "", 1, 60, 0},
127   {"apc0", "realm0", "trustrouter@peer0", 1, "tr.r0.apc0", "trustrouter@peer0", 0, 60, 0},
128   {"apc0", "realm1", "trustrouter@peer0", 0, "tr.r1.apc0", "trustrouter@peer0", 0, 60, 0},
129   {"apc0", "realm2", "trustrouter@peer0", 0, "tr.r2.apc0", "trustrouter@peer0", 1, 60, 0},
130   {"apc0", "realm3", "trustrouter@peer0", 1, "tr.r3.apc0", "trustrouter@peer0", 0, 60, 0},
131   {"apc0", "realm4", "trustrouter@peer0", 2, "tr.r4.apc0", "trustrouter@peer0", 0, 60, 0},
132   {"apc0", "realm0", "trustrouter@peer1", 0, "tr.r0.apc0", "trustrouter@peer1", 0, 60, 0},
133   {"apc0", "realm1", "trustrouter@peer1", 1, "tr.r1.apc0", "trustrouter@peer1", 0, 60, 0},
134   {"apc0", "realm2", "trustrouter@peer1", 1, "tr.r2.apc0", "trustrouter@peer1", 0, 60, 0},
135   {"apc0", "realm3", "trustrouter@peer1", 0, "tr.r3.apc0", "trustrouter@peer1", 1, 60, 0},
136   {"apc0", "realm4", "trustrouter@peer1", 2, "tr.r4.apc0", "trustrouter@peer1", 0, 60, 0},
137   {"apc0", "realm0", "trustrouter@peer2", 0, "tr.r0.apc0", "trustrouter@peer2", 0, 60, 0},
138   {"apc0", "realm1", "trustrouter@peer2", 2, "tr.r1.apc0", "trustrouter@peer2", 0, 60, 0},
139   {"apc0", "realm2", "trustrouter@peer2", 2, "tr.r2.apc0", "trustrouter@peer2", 0, 60, 0},
140   {"apc0", "realm3", "trustrouter@peer2", 1, "tr.r3.apc0", "trustrouter@peer2", 0, 60, 0},
141   {"apc0", "realm4", "trustrouter@peer2", 0, "tr.r4.apc0", "trustrouter@peer2", 1, 60, 0},
142 };
143 static size_t n_routes=sizeof(route_table)/sizeof(route_table[0]);
144
145 #if VERIFY_UPDATES
146 /* These are the correct updates to select from the above route table for each peer.
147  * The rule is: send selected route unless it is through that peer, otherwise send
148  * the best (lowest metric) alternative route. 
149  *
150  * In a few cases there are multiple valid options (when a two non-selected routes
151  * exist). If these tests are failing, it may be that the trps code is selecting another
152  * valid option, so check that. Probably ought to tweak metrics to avoid that ambiguity. */
153 static struct route_data update_table[][10]={
154   { /* peer0 */
155     {"apc0", "realm0", "", 0, "tr.r0.apc0", "", 1, 60, 0},
156     {"apc0", "realm1", "", 0, "tr.r1.apc0", "", 1, 60, 0},
157     {"apc0", "realm2", "trustrouter@peer1", 1, "tr.r2.apc0", "trustrouter@peer1", 0, 60, 0},
158     {"apc0", "realm3", "trustrouter@peer1", 0, "tr.r3.apc0", "trustrouter@peer1", 1, 60, 0},
159     {"apc0", "realm4", "trustrouter@peer2", 0, "tr.r4.apc0", "trustrouter@peer2", 1, 60, 0},
160     {NULL}
161   },
162   { /* peer1 */
163     {"apc0", "realm0", "", 0, "tr.r0.apc0", "", 1, 60, 0},
164     {"apc0", "realm1", "", 0, "tr.r1.apc0", "", 1, 60, 0},
165     {"apc0", "realm2", "trustrouter@peer0", 0, "tr.r2.apc0", "trustrouter@peer0", 1, 60, 0},
166     {"apc0", "realm3", "trustrouter@peer2", 1, "tr.r3.apc0", "trustrouter@peer2", 0, 60, 0},
167     {"apc0", "realm4", "trustrouter@peer2", 0, "tr.r4.apc0", "trustrouter@peer2", 1, 60, 0},
168     {NULL}
169   },
170   { /* peer2 */
171     {"apc0", "realm0", "", 0, "tr.r0.apc0", "", 1, 60, 0},
172     {"apc0", "realm1", "", 0, "tr.r1.apc0", "", 1, 60, 0},
173     {"apc0", "realm2", "trustrouter@peer0", 0, "tr.r2.apc0", "trustrouter@peer0", 1, 60, 0},
174     {"apc0", "realm3", "trustrouter@peer1", 0, "tr.r3.apc0", "trustrouter@peer1", 1, 60, 0},
175     {"apc0", "realm4", "trustrouter@peer1", 2, "tr.r4.apc0", "trustrouter@peer1", 0, 60, 0},
176     {NULL}
177   },
178   { /* peer3 */
179     {"apc0", "realm0", "", 0, "tr.r0.apc0", "", 1, 60, 0},
180     {"apc0", "realm1", "", 0, "tr.r1.apc0", "", 1, 60, 0},
181     {"apc0", "realm2", "trustrouter@peer0", 0, "tr.r2.apc0", "trustrouter@peer0", 1, 60, 0},
182     {"apc0", "realm3", "trustrouter@peer1", 0, "tr.r3.apc0", "trustrouter@peer1", 1, 60, 0},
183     {"apc0", "realm4", "trustrouter@peer2", 0, "tr.r4.apc0", "trustrouter@peer2", 1, 60, 0},
184     {NULL}
185   },
186   { /* peer4 */
187     {"apc0", "realm0", "", 0, "tr.r0.apc0", "", 1, 60, 0},
188     {"apc0", "realm1", "", 0, "tr.r1.apc0", "", 1, 60, 0},
189     {"apc0", "realm2", "trustrouter@peer0", 0, "tr.r2.apc0", "trustrouter@peer0", 1, 60, 0},
190     {"apc0", "realm3", "trustrouter@peer1", 0, "tr.r3.apc0", "trustrouter@peer1", 1, 60, 0},
191     {"apc0", "realm4", "trustrouter@peer2", 0, "tr.r4.apc0", "trustrouter@peer2", 1, 60, 0},
192     {NULL}
193   }
194 };
195 #endif /* VERIFY_UPDATES */
196
197 static void populate_rtable(TRPS_INSTANCE *trps)
198 {
199   int i;
200   TRP_ROUTE *new;
201
202   for (i=0; i<n_routes; i++) {
203     new=trp_route_new(NULL);
204     assert(new!=NULL);
205     trp_route_set_comm(new, tr_new_name(route_table[i].apc));
206     trp_route_set_realm(new, tr_new_name(route_table[i].realm));
207     trp_route_set_peer(new, tr_new_name(route_table[i].peer));
208     trp_route_set_metric(new, route_table[i].metric);
209     trp_route_set_trust_router(new, tr_new_name(route_table[i].trust_router));
210     trp_route_set_next_hop(new, tr_new_name(route_table[i].next_hop));
211     trp_route_set_selected(new, route_table[i].selected);
212     trp_route_set_interval(new, route_table[i].interval);
213     /* do not set expiry */
214     trp_rtable_add(trps->rtable, new);
215     new=NULL;
216   }
217 }
218
219 #if VERIFY_UPDATES
220 static void verify_update(TRP_ROUTE **updates, size_t n_updates, struct route_data *expected)
221 {
222   int ii,jj;
223   int found;
224
225   for(jj=0; jj<n_updates; jj++) {
226     found=0;
227     for (ii=0; expected[ii].apc!=NULL; ii++) {
228       if ((0==strcmp(expected[ii].apc, updates[jj]->comm->buf))
229          &&(0==strcmp(expected[ii].realm, updates[jj]->realm->buf))
230          &&(0==strcmp(expected[ii].peer, updates[jj]->peer->buf))
231          &&(expected[ii].metric==updates[jj]->metric)
232          &&(0==strcmp(expected[ii].trust_router, updates[jj]->trust_router->buf))
233          &&(0==strcmp(expected[ii].next_hop, updates[jj]->next_hop->buf))
234          &&(expected[ii].selected==updates[jj]->selected)
235          &&(expected[ii].interval==updates[jj]->interval)) {
236         assert(expected[ii].verified==0); /* should only encounter each entry once */
237         expected[ii].verified=1;
238         found=1;
239         continue;
240       }
241     }
242     if (!found) {
243       printf("missing:\n%s\n", trp_route_to_str(NULL,updates[jj], " | "));
244       assert(0);
245     }
246   }
247   for(ii=0; expected[ii].apc!=NULL; ii++)
248     assert(expected[ii].verified==1);
249 }
250
251 static void verify_update_selection(TRPS_INSTANCE *trps)
252 {
253   int ii;
254   TRP_ROUTE **updates=NULL;
255   size_t n_updates;
256   TR_NAME *gssname=NULL;
257   char *s;
258
259   for (ii=0; ii<n_peers; ii++) {
260     assert(0<asprintf(&s, "trustrouter@%s", peer_data[ii].server));
261     assert(NULL!=(gssname=tr_new_name(s)));
262     free(s);
263
264     updates=trps_select_updates_for_peer(NULL, trps, gssname, &n_updates);
265     tr_free_name(gssname);
266     verify_update(updates, n_updates, update_table[ii]);
267     talloc_free(updates);
268   }
269 }
270 #endif /* VERIFY_UPDATES */
271
272 int main(void)
273 {
274   TALLOC_CTX *main_ctx=talloc_new(NULL);
275   TRPS_INSTANCE *trps;
276   char *s;
277
278   trps=trps_new(main_ctx);
279
280   printf("\nPopulating peer table...\n");
281   populate_ptable(trps);
282
283   printf("\nVerifying peer table...\n");
284   verify_ptable(trps);
285
286   printf("\nPopulating route table...\n");
287   populate_rtable(trps);
288   s=trp_rtable_to_str(main_ctx, trps->rtable, " | ", NULL);
289   printf("Route Table:\n%s---\n", s);
290
291 #if VERIFY_UPDATES
292   printf("\nVerifying route update selection...\n");
293   verify_update_selection(trps);
294 #endif /* VERIFY_UPDATES */
295
296   printf("\nDone\n\n");
297   talloc_report_full(main_ctx, stderr);
298   talloc_free(main_ctx);
299   talloc_report_full(NULL, stderr);
300   return 0;
301 }