]>
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"
34 * Information that is kept for each node in the radix tree.
39 * Human readable representation of the string. Allocated using
48 * Add the given prefix (passed in as a string) to the given table.
50 static void add_node(struct bgp_table
*table
, const char *prefix_str
)
53 struct test_node_t
*node
;
58 if (str2prefix_ipv4(prefix_str
, &p
) <= 0)
61 rn
= bgp_node_get(table
, (struct prefix
*)&p
);
67 node
= malloc(sizeof(struct test_node_t
));
69 node
->prefix_str
= strdup(prefix_str
);
70 assert(node
->prefix_str
);
74 static void print_range_result(struct list
*list
)
77 struct listnode
*listnode
;
78 struct bgp_node
*bnode
;
80 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, bnode
)) {
81 char buf
[PREFIX2STR_BUFFER
];
83 prefix2str(&bnode
->p
, buf
, PREFIX2STR_BUFFER
);
88 static void check_lookup_result(struct list
*list
, va_list arglist
)
91 unsigned int prefix_count
= 0;
93 printf("Searching results\n");
94 while ((prefix_str
= va_arg(arglist
, char *))) {
95 struct listnode
*listnode
;
96 struct bgp_node
*bnode
;
101 printf("Searching for %s\n", prefix_str
);
103 if (str2prefix(prefix_str
, &p
) <= 0)
106 for (ALL_LIST_ELEMENTS_RO(list
, listnode
, bnode
)) {
107 if (prefix_same(&bnode
->p
, &p
))
114 printf("Checking for unexpected result items\n");
115 printf("Expecting %d found %d\n", prefix_count
, listcount(list
));
116 assert(prefix_count
== listcount(list
));
119 static void do_test(struct bgp_table
*table
, const char *prefix
,
120 uint32_t maxlen
, ...)
123 struct list
*list
= list_new();
126 list
->del
= (void (*)(void *))bgp_unlock_node
;
128 va_start(arglist
, maxlen
);
129 printf("\nDoing lookup for %s-%d\n", prefix
, maxlen
);
130 if (str2prefix(prefix
, &p
) <= 0)
132 bgp_table_range_lookup(table
, &p
, maxlen
, list
);
133 print_range_result(list
);
135 check_lookup_result(list
, arglist
);
137 list_delete_and_null(&list
);
141 printf("Checks successfull\n");
147 static void test_range_lookup(void)
149 struct bgp_table
*table
= bgp_table_init(NULL
, AFI_IP
, SAFI_UNICAST
);
151 printf("Testing bgp_table_range_lookup\n");
153 printf("Setup bgp_table");
154 const char *prefixes
[] = {"1.16.0.0/16", "1.16.128.0/18",
155 "1.16.192.0/18", "1.16.64.0/19",
156 "1.16.160.0/19", "1.16.32.0/20",
157 "1.16.32.0/21", "16.0.0.0/16"};
159 int num_prefixes
= sizeof(prefixes
) / sizeof(prefixes
[0]);
161 for (int i
= 0; i
< num_prefixes
; i
++)
162 add_node(table
, prefixes
[i
]);
164 do_test(table
, "1.16.0.0/17", 20, "1.16.64.0/19", "1.16.32.0/20", NULL
);
165 do_test(table
, "1.16.128.0/17", 20, "1.16.128.0/18", "1.16.192.0/18",
166 "1.16.160.0/19", NULL
);
168 do_test(table
, "1.16.128.0/17", 20, "1.16.128.0/18", "1.16.192.0/18",
169 "1.16.160.0/19", NULL
);
171 do_test(table
, "1.16.0.0/16", 18, "1.16.0.0/16", "1.16.128.0/18",
172 "1.16.192.0/18", NULL
);
174 do_test(table
, "1.16.0.0/16", 21, "1.16.0.0/16", "1.16.128.0/18",
175 "1.16.192.0/18", "1.16.64.0/19", "1.16.160.0/19",
176 "1.16.32.0/20", "1.16.32.0/21", NULL
);
178 do_test(table
, "1.17.0.0/16", 20, NULL
);
180 do_test(table
, "128.0.0.0/8", 16, NULL
);
182 do_test(table
, "16.0.0.0/8", 16, "16.0.0.0/16", NULL
);
184 do_test(table
, "0.0.0.0/3", 21, "1.16.0.0/16", "1.16.128.0/18",
185 "1.16.192.0/18", "1.16.64.0/19", "1.16.160.0/19",
186 "1.16.32.0/20", "1.16.32.0/21", "16.0.0.0/16", NULL
);