]>
git.proxmox.com Git - mirror_frr.git/blob - tests/bgpd/test_bgp_table.c
2 * BGP Routing table range lookup test
3 * Copyright (C) 2012 OSR.
4 * Copyright (C) 2018 Marcel Röthke (marcel.roethke@haw-hamburg.de), for HAW
7 * This file is part of FRRouting
9 * Quagga is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the
11 * Free Software Foundation; either version 2, or (at your option) any
14 * Quagga is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; see the file COPYING; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 #include "bgpd/bgp_table.h"
31 /* Satisfy link requirements from including bgpd.h */
32 struct zebra_privs_t bgpd_privs
= {0};
36 * Information that is kept for each node in the radix tree.
41 * Human readable representation of the string. Allocated using
50 * Add the given prefix (passed in as a string) to the given table.
52 static void add_node(struct bgp_table
*table
, const char *prefix_str
)
55 struct test_node_t
*node
;
60 if (str2prefix_ipv4(prefix_str
, &p
) <= 0)
63 rn
= bgp_node_get(table
, (struct prefix
*)&p
);
69 node
= malloc(sizeof(struct test_node_t
));
71 node
->prefix_str
= strdup(prefix_str
);
72 assert(node
->prefix_str
);
76 static void print_range_result(struct list
*list
)
79 struct listnode
*listnode
;
80 struct bgp_node
*bnode
;
82 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, bnode
)) {
83 char buf
[PREFIX2STR_BUFFER
];
85 prefix2str(&bnode
->p
, buf
, PREFIX2STR_BUFFER
);
90 static void check_lookup_result(struct list
*list
, va_list arglist
)
93 unsigned int prefix_count
= 0;
95 printf("Searching results\n");
96 while ((prefix_str
= va_arg(arglist
, char *))) {
97 struct listnode
*listnode
;
98 struct bgp_node
*bnode
;
103 printf("Searching for %s\n", prefix_str
);
105 if (str2prefix(prefix_str
, &p
) <= 0)
108 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, bnode
)) {
109 if (prefix_same(&bnode
->p
, &p
))
116 printf("Checking for unexpected result items\n");
117 printf("Expecting %d found %d\n", prefix_count
, listcount(list
));
118 assert(prefix_count
== listcount(list
));
121 static void do_test(struct bgp_table
*table
, const char *prefix
,
122 uint32_t maxlen
, ...)
125 struct list
*list
= list_new();
128 list
->del
= (void (*)(void *))bgp_unlock_node
;
130 va_start(arglist
, maxlen
);
131 printf("\nDoing lookup for %s-%d\n", prefix
, maxlen
);
132 if (str2prefix(prefix
, &p
) <= 0)
134 bgp_table_range_lookup(table
, &p
, maxlen
, list
);
135 print_range_result(list
);
137 check_lookup_result(list
, arglist
);
143 printf("Checks successfull\n");
149 static void test_range_lookup(void)
151 struct bgp_table
*table
= bgp_table_init(NULL
, AFI_IP
, SAFI_UNICAST
);
153 printf("Testing bgp_table_range_lookup\n");
155 printf("Setup bgp_table");
156 const char *prefixes
[] = {"1.16.0.0/16", "1.16.128.0/18",
157 "1.16.192.0/18", "1.16.64.0/19",
158 "1.16.160.0/19", "1.16.32.0/20",
159 "1.16.32.0/21", "16.0.0.0/16"};
161 int num_prefixes
= sizeof(prefixes
) / sizeof(prefixes
[0]);
163 for (int i
= 0; i
< num_prefixes
; i
++)
164 add_node(table
, prefixes
[i
]);
166 do_test(table
, "1.16.0.0/17", 20, "1.16.64.0/19", "1.16.32.0/20", NULL
);
167 do_test(table
, "1.16.128.0/17", 20, "1.16.128.0/18", "1.16.192.0/18",
168 "1.16.160.0/19", NULL
);
170 do_test(table
, "1.16.128.0/17", 20, "1.16.128.0/18", "1.16.192.0/18",
171 "1.16.160.0/19", NULL
);
173 do_test(table
, "1.16.0.0/16", 18, "1.16.0.0/16", "1.16.128.0/18",
174 "1.16.192.0/18", NULL
);
176 do_test(table
, "1.16.0.0/16", 21, "1.16.0.0/16", "1.16.128.0/18",
177 "1.16.192.0/18", "1.16.64.0/19", "1.16.160.0/19",
178 "1.16.32.0/20", "1.16.32.0/21", NULL
);
180 do_test(table
, "1.17.0.0/16", 20, NULL
);
182 do_test(table
, "128.0.0.0/8", 16, NULL
);
184 do_test(table
, "16.0.0.0/8", 16, "16.0.0.0/16", NULL
);
186 do_test(table
, "0.0.0.0/3", 21, "1.16.0.0/16", "1.16.128.0/18",
187 "1.16.192.0/18", "1.16.64.0/19", "1.16.160.0/19",
188 "1.16.32.0/20", "1.16.32.0/21", "16.0.0.0/16", NULL
);