2 * Copyright (c) 2016, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
40 #include <tr_name_internal.h>
41 #include <trp_internal.h>
42 #include <trp_rtable.h>
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]);
51 static unsigned int metric1(size_t a, size_t b, size_t c)
56 static unsigned int metric2(size_t a, size_t b, size_t c)
61 static unsigned int metric3(size_t a, size_t b, size_t c)
66 static void populate_rtable(TRP_RTABLE *table, unsigned int (*metric)(size_t, size_t, size_t))
68 TRP_ROUTE *entry=NULL;
69 size_t ii=0, jj=0, kk=0;
70 struct timespec ts={0,0};
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 */
91 static void verify_rtable(TRP_RTABLE *table, unsigned int (*metric)(size_t, size_t, size_t))
93 TRP_ROUTE *entry=NULL;
94 size_t ii=0, jj=0, kk=0;
96 TR_NAME *apc_n, *realm_n, *peer_n;
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);
106 tr_free_name(realm_n);
107 tr_free_name(peer_n);
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));
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));
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));
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));
127 assert(trp_route_get_metric(entry)==metric(ii,jj,kk));
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));
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);
137 printf("{%s %s %s} entry OK!\n", apc[ii], realm[jj], peer[kk]);
143 static int is_in(char *a, char *b[], size_t n_b)
148 if (0==strcmp(a, b[n_b]))
153 static void verify_apc_list(TRP_RTABLE *table)
156 TR_NAME **apcs_found=trp_rtable_get_comms(table, &n);
159 assert(1==is_in(apcs_found[n]->buf, apc, n_apc));
162 static void verify_apc_realm_lists(TRP_RTABLE *table)
165 TR_NAME *apc_n=NULL, **realms_found=NULL;
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);
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]);
179 static void verify_get_apc_entries(TRP_RTABLE *table)
182 TRP_ROUTE **apc_entries=NULL;
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);
189 assert(n==n_realm*n_peer);
191 assert(0==strncmp(trp_route_get_comm(apc_entries[n])->buf,
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));
197 printf("APC %s ok!\n", apc[ii]);
198 talloc_free(apc_entries);
202 static void verify_get_realm_entries(TRP_RTABLE *table)
204 size_t n=0, ii=0, jj=0;
205 TRP_ROUTE **realm_entries=NULL;
206 TR_NAME *apc_n=NULL, *realm_n=NULL;
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);
214 tr_free_name(realm_n);
217 assert(0==strncmp(trp_route_get_comm(realm_entries[n])->buf,
219 trp_route_get_comm(realm_entries[n])->len));
220 assert(0==strncmp(trp_route_get_realm(realm_entries[n])->buf,
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));
225 printf("APC %s realm %s ok!\n", apc[ii], realm[jj]);
226 talloc_free(realm_entries);
231 /* doesn't work if c not in a */
232 static size_t get_index(char *c, char **a, size_t n_a)
235 if (0==strcmp(c, a[n_a]))
241 static void update_metric(TRP_RTABLE *table, unsigned int (*new_metric)(size_t, size_t, size_t))
243 TRP_ROUTE **entries=NULL;
244 size_t n=0, ii=0,jj=0,kk=0;
246 entries=trp_rtable_get_entries(table, &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));
254 talloc_free(entries);
257 static void remove_entries(TRP_RTABLE *table)
259 size_t n=trp_rtable_size(table);
261 TR_NAME *apc_n, *realm_n, *peer_n;
262 TRP_ROUTE *entry=NULL;
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);
273 tr_free_name(realm_n);
274 tr_free_name(peer_n);
275 trp_rtable_remove(table, entry);
277 assert(trp_rtable_size(table)==--n);
284 static void print_rtable(TRP_RTABLE *table)
286 char *s=trp_rtable_to_str(NULL, table, NULL, NULL);
293 TRP_RTABLE *table=NULL;
294 table=trp_rtable_new();
295 populate_rtable(table, metric1);
298 printf("\nVerifying routing table...\n");
299 verify_rtable(table, metric1);
300 printf(" ...success!\n");
302 printf("\nVerifying APC list...\n");
303 verify_apc_list(table);
304 printf(" ...success!\n");
306 printf("\nVerifying APC realm lists...\n");
307 verify_apc_realm_lists(table);
308 printf(" ...success!\n");
310 printf("\nVerifying APC entry lists...\n");
311 verify_get_apc_entries(table);
312 printf(" ...success!\n");
314 printf("\nVerifying realm entry lists...\n");
315 verify_get_realm_entries(table);
316 printf(" ...success!\n");
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");
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");
328 printf("\nVerifying element removal...\n");
329 remove_entries(table);
331 printf(" ...success!\n");
333 printf("\nRepopulating table...\n");
334 populate_rtable(table, metric3);
335 verify_rtable(table, metric3);
336 printf(" ...success!\n");
338 trp_rtable_free(table);