Add a 'request_id' to TID requests and responses
[trust_router.git] / trp / test / rtbl_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 <string.h>
38 #include <assert.h>
39
40 #include <tr_name_internal.h>
41 #include <trp_internal.h>
42 #include <trp_rtable.h>
43
44 char *apc[]={"apc1", "apc2", "apc3"};
45 size_t n_apc=sizeof(apc)/sizeof(apc[0]);
46 char *realm[]={"realm1", "realm2", "realm3"};
47 size_t n_realm=sizeof(realm)/sizeof(realm[0]);
48 char *peer[]={"peer1", "peer2", "peer3"};
49 size_t n_peer=sizeof(peer)/sizeof(peer[0]);
50
51 static unsigned int metric1(size_t a, size_t b, size_t c)
52 {
53   return a+b+c;
54 }
55
56 static unsigned int metric2(size_t a, size_t b, size_t c)
57 {
58   return a+b+c+25;
59 }
60
61 static unsigned int metric3(size_t a, size_t b, size_t c)
62 {
63   return a*(b+c)+b*c;
64 }
65
66 static void populate_rtable(TRP_RTABLE *table, unsigned int (*metric)(size_t, size_t, size_t))
67 {
68   TRP_ROUTE *entry=NULL;
69   size_t ii=0, jj=0, kk=0;
70   struct timespec ts={0,0};
71
72   for (ii=0; ii<n_apc; ii++) {
73     for (jj=0; jj<n_realm; jj++) {
74       for (kk=0; kk<n_peer; kk++) {
75         entry=trp_route_new(NULL);
76         trp_route_set_comm(entry, tr_new_name(apc[ii]));
77         trp_route_set_realm(entry, tr_new_name(realm[jj]));
78         trp_route_set_trust_router(entry, tr_new_name(realm[jj]));
79         trp_route_set_peer(entry, tr_new_name(peer[kk]));
80         trp_route_set_metric(entry, metric(ii,jj,kk));
81         trp_route_set_next_hop(entry, tr_new_name(peer[kk]));
82         ts=(struct timespec){jj+1,ii+kk+1};
83         trp_route_set_expiry(entry, &ts);
84         trp_rtable_add(table, entry);
85         entry=NULL; /* entry belongs to the table now */
86       }
87     }
88   }
89 }
90
91 static void verify_rtable(TRP_RTABLE *table, unsigned int (*metric)(size_t, size_t, size_t))
92 {
93   TRP_ROUTE *entry=NULL;
94   size_t ii=0, jj=0, kk=0;
95   size_t len=0;
96   TR_NAME *apc_n, *realm_n, *peer_n;
97
98   for (ii=0; ii<n_apc; ii++) {
99     for (jj=0; jj<n_realm; jj++) {
100       for (kk=0; kk<n_peer; kk++) {
101         apc_n=tr_new_name(apc[ii]);
102         realm_n=tr_new_name(realm[jj]);
103         peer_n=tr_new_name(peer[kk]);
104         entry=trp_rtable_get_entry(table, apc_n, realm_n, peer_n);
105         tr_free_name(apc_n);
106         tr_free_name(realm_n);
107         tr_free_name(peer_n);
108
109         assert(entry!=NULL);
110
111         len=trp_route_get_comm(entry)->len;
112         assert(len==strlen(apc[ii]));
113         assert(0==strncmp(trp_route_get_comm(entry)->buf, apc[ii], len));
114
115         len=trp_route_get_realm(entry)->len;
116         assert(len==strlen(realm[jj]));
117         assert(0==strncmp(trp_route_get_realm(entry)->buf, realm[jj], len));
118         
119         len=trp_route_get_peer(entry)->len;
120         assert(len==strlen(peer[kk]));
121         assert(0==strncmp(trp_route_get_peer(entry)->buf, peer[kk], len));
122         
123         len=trp_route_get_trust_router(entry)->len;
124         assert(len==strlen(realm[jj]));
125         assert(0==strncmp(trp_route_get_trust_router(entry)->buf, realm[jj], len));
126
127         assert(trp_route_get_metric(entry)==metric(ii,jj,kk));
128
129         len=trp_route_get_next_hop(entry)->len;
130         assert(len==strlen(peer[kk]));
131         assert(0==strncmp(trp_route_get_next_hop(entry)->buf, peer[kk], len));
132
133         assert(trp_route_is_selected(entry)==0);
134         assert(trp_route_get_expiry(entry)->tv_sec==jj+1);
135         assert(trp_route_get_expiry(entry)->tv_nsec==ii+kk+1);
136
137         printf("{%s %s %s} entry OK!\n", apc[ii], realm[jj], peer[kk]);
138       }
139     }
140   }
141 }
142
143 static int is_in(char *a, char *b[], size_t n_b)
144 {
145   size_t count=0;
146
147   while (n_b--) {
148     if (0==strcmp(a, b[n_b]))
149       count++;
150   }
151   return count;
152 }
153 static void verify_apc_list(TRP_RTABLE *table)
154 {
155   size_t n=0;
156   TR_NAME **apcs_found=trp_rtable_get_comms(table, &n);
157   assert(n==n_apc);
158   while(n--)
159     assert(1==is_in(apcs_found[n]->buf, apc, n_apc));
160 }
161
162 static void verify_apc_realm_lists(TRP_RTABLE *table)
163 {
164   size_t n=0, ii=0;
165   TR_NAME *apc_n=NULL, **realms_found=NULL;
166
167   for (ii=0; ii<n_apc; ii++) {
168     apc_n=tr_new_name(apc[ii]);
169     realms_found=trp_rtable_get_comm_realms(table, apc_n, &n);
170     tr_free_name(apc_n);
171     assert(n==n_realm);
172     while (n--)
173       assert(1==is_in(realms_found[n]->buf, realm, n_realm));
174     talloc_free(realms_found);
175     printf("APC %s ok!\n", apc[ii]);
176   }
177 }
178
179 static void verify_get_apc_entries(TRP_RTABLE *table)
180 {
181   size_t n=0, ii=0;
182   TRP_ROUTE **apc_entries=NULL;
183   TR_NAME *apc_n=NULL;
184
185   for (ii=0; ii<n_apc; ii++) {
186     apc_n=tr_new_name(apc[ii]);
187     apc_entries=trp_rtable_get_comm_entries(table, apc_n, &n);
188     tr_free_name(apc_n);
189     assert(n==n_realm*n_peer);
190     while (n--) {
191       assert(0==strncmp(trp_route_get_comm(apc_entries[n])->buf,
192                         apc[ii],
193                         trp_route_get_comm(apc_entries[n])->len));
194       assert(1==is_in(trp_route_get_realm(apc_entries[n])->buf, realm, n_realm));
195       assert(1==is_in(trp_route_get_peer(apc_entries[n])->buf, peer, n_peer));
196     }
197     printf("APC %s ok!\n", apc[ii]);
198     talloc_free(apc_entries);
199   }
200 }
201
202 static void verify_get_realm_entries(TRP_RTABLE *table)
203 {
204   size_t n=0, ii=0, jj=0;
205   TRP_ROUTE **realm_entries=NULL;
206   TR_NAME *apc_n=NULL, *realm_n=NULL;
207
208   for (ii=0; ii<n_apc; ii++) {
209     for (jj=0; jj<n_realm; jj++) {
210       apc_n=tr_new_name(apc[ii]);
211       realm_n=tr_new_name(realm[jj]);
212       realm_entries=trp_rtable_get_realm_entries(table, apc_n, realm_n, &n);
213       tr_free_name(apc_n);
214       tr_free_name(realm_n);
215       assert(n==n_peer);
216       while (n--) {
217         assert(0==strncmp(trp_route_get_comm(realm_entries[n])->buf,
218                           apc[ii],
219                           trp_route_get_comm(realm_entries[n])->len));
220         assert(0==strncmp(trp_route_get_realm(realm_entries[n])->buf,
221                           realm[jj],
222                           trp_route_get_realm(realm_entries[n])->len));
223         assert(1==is_in(trp_route_get_peer(realm_entries[n])->buf, peer, n_peer));
224       }
225       printf("APC %s realm %s ok!\n", apc[ii], realm[jj]);
226       talloc_free(realm_entries);
227     }
228   }
229 }
230
231 /* doesn't work if c not in a */
232 static size_t get_index(char *c, char **a, size_t n_a)
233 {
234   while(n_a--) {
235     if (0==strcmp(c, a[n_a]))
236       return n_a;
237   }
238   return 0;
239 }
240
241 static void update_metric(TRP_RTABLE *table, unsigned int (*new_metric)(size_t, size_t, size_t))
242 {
243   TRP_ROUTE **entries=NULL;
244   size_t n=0, ii=0,jj=0,kk=0;
245
246   entries=trp_rtable_get_entries(table, &n);
247   while (n--) {
248     ii=get_index(trp_route_get_comm(entries[n])->buf, apc, n_apc);
249     jj=get_index(trp_route_get_realm(entries[n])->buf, realm, n_realm);
250     kk=get_index(trp_route_get_peer(entries[n])->buf, peer, n_peer);
251     trp_route_set_metric(entries[n],
252                           new_metric(ii,jj,kk));
253   }
254   talloc_free(entries);
255 }
256
257 static void remove_entries(TRP_RTABLE *table)
258 {
259   size_t n=trp_rtable_size(table);
260   size_t ii,jj,kk;
261   TR_NAME *apc_n, *realm_n, *peer_n;
262   TRP_ROUTE *entry=NULL;
263
264   for (ii=0; ii<n_apc; ii++) {
265     for (jj=0; jj<n_realm; jj++) {
266       for (kk=0; kk<n_realm; kk++) {
267         apc_n=tr_new_name(apc[ii]);
268         realm_n=tr_new_name(realm[jj]);
269         peer_n=tr_new_name(peer[kk]);
270         entry=trp_rtable_get_entry(table, apc_n, realm_n, peer_n);
271         assert(entry !=NULL);
272         tr_free_name(apc_n);
273         tr_free_name(realm_n);
274         tr_free_name(peer_n);
275         trp_rtable_remove(table, entry);
276         entry=NULL;
277         assert(trp_rtable_size(table)==--n);
278       }
279     }
280   }
281 }
282
283
284 static void print_rtable(TRP_RTABLE *table)
285 {
286   char *s=trp_rtable_to_str(NULL, table, NULL, NULL);
287   printf("%s",s);
288   talloc_free(s);
289 }
290
291 int main(void)
292 {
293   TRP_RTABLE *table=NULL;
294   table=trp_rtable_new();
295   populate_rtable(table, metric1);
296   print_rtable(table);
297
298   printf("\nVerifying routing table...\n");
299   verify_rtable(table, metric1);
300   printf("                         ...success!\n");
301
302   printf("\nVerifying APC list...\n");
303   verify_apc_list(table);
304   printf("                    ...success!\n");
305
306   printf("\nVerifying APC realm lists...\n");
307   verify_apc_realm_lists(table);
308   printf("                    ...success!\n");
309
310   printf("\nVerifying APC entry lists...\n");
311   verify_get_apc_entries(table);
312   printf("                           ...success!\n");
313
314   printf("\nVerifying realm entry lists...\n");
315   verify_get_realm_entries(table);
316   printf("                              ...success!\n");
317
318   printf("\nVerifying table value update...\n");
319   update_metric(table, metric2); /* changes the metric value in each element in-place */
320   verify_rtable(table, metric2);
321   printf("                              ...success!\n");
322
323   printf("\nVerifying element replacement...\n");
324   populate_rtable(table, metric3); /* replaces all the elements with new ones */
325   verify_rtable(table, metric3);
326   printf("                               ...success!\n");
327
328   printf("\nVerifying element removal...\n");
329   remove_entries(table);
330   print_rtable(table);
331   printf("                           ...success!\n");
332
333   printf("\nRepopulating table...\n");
334   populate_rtable(table, metric3);
335   verify_rtable(table, metric3);
336   printf("                               ...success!\n");
337
338   trp_rtable_free(table);
339   return 0;
340 }